PHP array_push() Function
Example
Insert "blue" and "yellow" to the end of an array:
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
Try it Yourself »
Definition and Usage
The array_push() function inserts one or more elements to the end of an array.
Tip: You can add one value, or as many as you like.
Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).
Syntax
array_push(array, value1, value2, ...)
Parameter Values
Parameter | Description |
---|---|
array | Required. Specifies an array |
value1 | Optional. Specifies the value to add (Required in PHP versions before 7.3) |
value2 | Optional. Specifies the value to add |
Technical Details
Return Value: | Returns the new number of elements in the array |
---|---|
PHP Version: | 4+ |
Change log: | As of version 7.3 this function can be called with only the array parameter |
More Examples
Example
An array with string keys:
<?php
$a=array("a"=>"red","b"=>"green");
array_push($a,"blue","yellow");
print_r($a);
?>
Try it Yourself »
❮ PHP Array Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.