PHP array push

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: array_push() function

PHP: Push one or more elements onto the end of array

The array_push() function is used to add one or more elements onto the end of an array. The length of array increases by the number of variables pushed.

Version

(PHP 4 and above)

Syntax

array_push(array_name, value1, value2...)

Parameters

Name

Description

Required /
Optional

Type

array_name

The input array

Required

Array

value1

Value to add.

Required

Mixed*

value2..

Value to add.

Optional

Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

The new number of elements inserted in array_name.

Value Type : Integer.

Pictorial Presentation :

php-array-array_push()

Example :

  1. $array1= array('Mathematics','Physics');  

  2. array_push($array1,'Chemistry','Biology');  

  3. print_r($array1);  

  4. ?>  

Output :

Array ( [0] => Mathematics [1] => Physics [2] => Chemistry [3] => Biology )