Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

array_push()

Example

Append “blue” and “yellow” to the end of an array:

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

Definition and Usage

The array_push() function adds one or more elements to the end of an array.

Tip: You can add a single value or multiple values.

Note: Even if the array has string keys, the added elements will always have numeric keys (see example below).

Syntax

array_push(array, value1, value2, …)

Parameter Values

Parameter

Description

array

Required. Defines the array.

value1

Optional. Specifies the value to add (required in PHP versions prior to 7.3).

value2

Optional. Indicates the value to be added.

Technical Details

 

Return Value:

Returns the updated count of elements in the array.

PHP Version:

4+

Change log:

Starting from version 7.3, this function can be invoked with just the array parameter.

More Examples

Example

An array with string indices:

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