Latest web development tutorials

PHP array_push () function

PHP Array Reference Complete PHP Array Reference

Examples

To the end of the array insert "blue" and "yellow":

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

Running instance »

Definition and Usage

array_push () function inserts one or more elements to the end of the array.

Tip: You can add one or more values.

NOTE: Even if you have an array of string keys, add elements that you will be numeric keys (see example below).


grammar

array_push( array,value1,value2... )

参数 描述
array 必需。规定一个数组。
value1 必需。规定要添加的值。
value2 可选。规定要添加的值。

technical details

return value: Returns the number of elements in the new array.
PHP version: 4+


More examples

Example 1

With a string of key names array:

<?php
$a=array("a"=>"red","b"=>"green");
array_push($a,"blue","yellow");
print_r($a);
?>

Running instance »


PHP Array Reference Complete PHP Array Reference