Question – How to Append an Item to Array in PHP. How do I append any element to end of the existing Array in PHP? How to push element to array in PHP?
This tutorial uses array_push() function to insert or append a new element to end of the Array.
Advertisement
PHP – Append Element to Array
The following example creates an initial array with two elements (as “black”,”blue”). After that add use array_push() function to append new element “white” to the array.
1 2 3 4 5 6 7 | <?php $arr = array("black","blue"); array_push($arr, "white"); //View final array print_r($arr); ?> |
Output:
Array ( [0] => black [1] => blue [2] => white )