comments
Author: Admin/Publisher |finished | checked
PHP array_push function
The PHP array_push function adds one or more values to the end of an array. As you can see in the description, the array is passed by reference, meaning that this function will modify the original array.
Description / Descripciónarray_push(array &$array, mixed ...$values): int
The array_push
function has the same effect as $array[] = $var;
for each value we are adding.
Note: This function will give a warning if the first argument is an array starting from PHP 7.1.
Parameters
$array
– the array to which we will add the values or value.
$values
– the values or value that will be added to the end of the array.
Return
int
Returns the number of elements entered into the array.
PHP array_push example
This is a simple function; it doesn’t need more than one example to understand it.
// Crear un array $frutas = array("manzana", "banana", "uva"); // Mostrar el array antes de agregar elementos echo "Array antes de agregar elementos: "; print_r($frutas); // Agregar elementos al final del array usando array_push array_push($frutas, "naranja", "kiwi"); // Mostrar el array después de agregar elementos echo "Array después de agregar elementos: "; print_r($frutas);
Array antes de agregar elementos: Array ( [0] => manzana [1] => banana [2] => uva ) Array después de agregar elementos: Array ( [0] => manzana [1] => banana [2] => uva [3] => naranja [4] => kiwi )
Category: en-php
Something wrong?
If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category
Recommendations:
If you have to add multiple values to an array, use
array_push
; otherwise, use$array[] =
.Many times, you will use
$array[] =
instead ofarray_push
, for example when iterating over another array and obtaining values that you want to store in a new array.