Code has been added to clipboard!

PHP array_push

Reading time 1 min
Published Aug 14, 2017
Updated Oct 10, 2019

PHP array push: Main Tips

  • PHP array_push() function is used to insert new elements into the end of an array and get the updated number of array elements.
  • You may add as many values as you need.
  • Your added elements will always have numeric keys, even if the array itself has string keys.
  • PHP array push function has been introduced in PHP 4.

array_push() Explained

This function is used to insert elements into the end of an array. Here is a simple example to get you started:

Example
<?php
  $z = ['me','you', 'he'];
  array_push($z, 'she', 'it');
  print_r($z);
?>

Learn Correct Syntax

To have PHP array push function executed correctly, you have to follow this syntax:

array_push(array,value1,value2...);

In the table below, the parameters used in the PHP array push function are explained one by one:

Parameter Description
array Necessary. Defines an array.
value1 Necessary. Defines the value to add.
value2 Optional. Defines the next values to add.

Example

Look at the code below. You can see an array that contains string keys. Let's see how two new values are added at the end of it using PHP array push function:

Example
<?php
  $z = [
    'Drago' => 'blue',
    'Rex' => 'brown'
  ];
  array_push($z, 'black', 'purple');
  print_r($z);
?>
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()