How to Add Elements to an Empty Array in PHP
array_push()
You can simply use the array_push() function to add new elements or values to an empty PHP array.
Let's take a look at an example to understand how it basically works:
<?php // Adding values one by one $array1 = array(); array_push($array1, 1); array_push($array1, 2); array_push($array1, 3); print_r($array1); echo "<br>"; // Adding all values at once $array2 = array(); array_push($array2, 1, 2, 3); print_r($array2); echo "<br>"; // Adding values through loop $array3 = array(); for($i=1; $i<=3; $i++){ $array3[] = $i; } print_r($array3); ?>
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Use the
array_push()FunctionYou can simply use the
array_push()function to add new elements or values to an empty PHP array.Let's take a look at an example to understand how it basically works:
need an explanation for this answer? contact us directly to get an explanation for this answer