⊗ppPmLpALF 127 of 447 menu

Filling Arrays in PHP

Let's say we have some array:

<?php $arr = []; ?>

Let's fill this array with some elements. It can be done like this:

<?php $arr = [1, 2, 3, 4, 5]; ?>

Alternatively, you can initially create an empty array, and then add data to it like this:

<?php $arr = []; $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 4; $arr[] = 5; ?>

Obviously, this method is not very convenient, especially if there are a lot of elements in the array. Let's rewrite our code so that the array filling is done by a for loop:

<?php $arr = []; for ($i = 1; $i <= 5; $i++) { $arr[] = $i; } var_dump($arr); ?>

Using a loop, fill an array with numbers from 1 to 100.

Using a loop, fill an array with odd numbers in the range from 1 to 99.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline