X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

PHP array_push() Function

PHP Array Reference PHP Array Reference

Example

Insert "blue" and "yellow" to the end of an array:

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

Run example »

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 Description
array Required. Specifies an array
value1 Required. Specifies the value to add
value2 Optional. Specifies the value to add

Technical Details

Return Value: Returns the new number of elements in the array
PHP Version: 4+


More Examples

Example 1

An array with string keys:

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

Run example »


PHP Array Reference PHP Array Reference

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook