The array_push function adds/inserts a new element to the end of the array. You can add single or multiple values at a time.
In the case of an associative array, if your array contains string keys, the values inserted by the array_push function will always have numeric keys.
What is the syntax of the array_push function in PHP?
array_push(array, value1, value2, ...)
Parameters | Details |
---|---|
array | Array to push the value to – Required |
value1 | Value to add – Required |
value2 | Next value to add – Optional |
Examples of array_push function in PHP
<?php
$arr=array("blue","red");
array_push($arr,"green","orange");
print_r($arr);
?>
In the above example, we insert two new values to the end of the array.
<?php
$arr=array("a"=>"1","b"=>"2");
array_push($arr,"4","6");
print_r($arr);
?>
In the above example, we insert new values to the end of the associative array. Although, the array contains string keys our new inserted values will contain numeric kets.