php add to array
php add to array
1 Answer
add Element to the end of array using array_push($arrayName, Val1 , Val2 ,etc ..)
Example
<?php
echo'<pre>';
$array = [1,2,3];
array_push($array,4,5);
print_r($array);
echo'</pre>';
?>
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
when keys are not important $arrayName [ ] = Val ; in this case, the value of the key will be plus one more than the last integer keys.
You can put the key-value integer or text within the brackets [ ]
If the key name is repeated, it will change the value to the new value and will not cause any problems.
Example
<?php
echo'<pre>';
$array = [1,2,3];
$array[88] = 4;
$array['QA'] = 5;
$array[] = 6;
print_r($array);
echo'</pre>';
?>
Array ( [0] => 1 [1] => 2 [2] => 3 [88] => 4 [QA] => 5 [89] => 6 )
answer Link