PHP array_push

 PHP array_push is used to hang one or more elements and their values behind the array ( PHP Array ), sounds a bit foggy? To put it in a simpler way, array_push can be used to increase the size of an array, or even add another array to the original array.


PHP array_push syntax example

array_push (original array, $var to be added [, $var to be added...])


Use array_push to hang the $var to be added to the end of the original array. You can continue to hang many $vars. Here, $var can be a variable or another array. If you need to add $var repeatedly, you can use PHP For loop or PHP while loop to help you deal with.

PHP array_push implementation example
<?php
$OrigiArray= array("A", "B");
array_push($OrigiArray, "C", "D");
print_r($OrigiArray);
?>
Output result: Array ([0] => A [1] => B [2] => C [3] => D)

Originally, the $OrigiArray array has only two array elements, and C and D are linked to by array_push At the back of $OrigiArray, you can expand $OrigiArray into four array elements. This is simply adding a text array element to the array. You can also try adding another array.

Note: When the first element of array_push is not an array, it will not work normally. In order to avoid such an error condition, you can usually create an array first, which can be an empty array or an array that already has elements, and then Throw it to array_push for processing.

PS. If you just want to create a simple Array, you can directly use $array[ ]= . On the one hand, it is easier to write, on the other hand, PHP does not need to call an array first and then perform the expansion, saving System additional resource expenditure.

Post a Comment

0 Comments