PHP array_push() function

P
PHP array_product() function
PHP array_rand() function

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, ...)
ParametersDetails
arrayArray to push the value to – Required
value1Value to add – Required
value2Next value to add – Optional
array_push function in PHP

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.

PHP array_product() function
PHP array_rand() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z