PHP: array_push Function | PHP Add to Array Tutorial

By Parth Patel on Oct 26, 2020

Today, we will talk about an array function in PHP called array_push which is used to push elements into an array.

We will learn:

  • How to add single item to array in php
  • How to add multiple items to array in php
  • How to push array inside array in php

Understanding array_push function

The array_push() is used to insert new elements at the end of given array. It can accept any numbers of elements to be pushed at the end of the array. Let's see an example:

Syntax

array_push( array &$array, $value1, $value2 ....)

Parameters:

  1. The array_push() function accepts array as the first argument [Required and array type]
  2. The second parameter ($value1) can be any value to be pushed to the end of the array [Required]
  3. Third and subsequent parameters are optional and defines the next values to be pushed to the array [Optional]

Example

$num = ['one','two', 'three'];
array_push($num, 'four', 'five');
print_r($num);
// ['one','two', 'three','four', 'five']

Let's review some different use-cases where you would need to use array_push() function to add items to an array:

Adding single item to an array

Most basic use-case is to add just one element to an array in php. For that, you can use array_push() function as below:

$flowers = ['lily','rose'];
array_push($flowers, 'sunflower');
print_r($flowers);
// ['lily', 'rose', 'sunflower']

But,

if you just want to add single item to an array then you don't need to use array_push() function as there is more efficient way to push single item to an array:

$flowers = ['lily', 'rose'];
$flowers[] = 'sunflower';
print_r($flowers);
// ['lily', 'rose', 'sunflower']

Using [] operator to push single item to an array is more efficient as there is no overhead of calling a function.

Also Read: Convert String to Array in PHP

Adding multiple items to an array

If you want to push multiple elements to an array, then array_push() is perfect function for that. It will append all items to the end of an array in the order you passed. Let's check out an example:

$flowers = ['lily', 'rose'];
array_push($flowers, 'sunflower', 'orchid');
print_r($flowers);
// ['lily', 'rose', 'sunflower', 'orchid']

Adding array to an array

You can also pass an array inside an array. An array can still be a value, right?

Let's see an example:

$flowers = ['lily', 'rose'];
array_push($flowers, ['name' => 'sunflower', 'type' => 'flower']);
print_r($flowers);
// output
[
	0 => 'lily',
	1 => 'rose',
	2 => [
					'name' => 'sunflower',
					'type' => 'flower'
				]
]

Here, at 3rd index, whole array has been assigned. Thus, in PHP array, you can mix-match different types of values including arrays.

Also Read: How to generate Unique ID in PHP

Adding an item to multidimensional array

In previous section, we learnt how to push an array inside another array. What if you want to add an item to the inner array? That's actually called multi-dimensional array.

Let's see how we can add item to multi-dimensional array:

$marray = [4,5,['one','two'] ];
//let's add an item to sunflower array

array_push($marray[2], 'three', 'four'];
print_r($marray);

//output
[ 4, 5, ['one', 'two', 'three', 'four'] ]

Pushing key and value in Associative array

The array_push() doesn't support setting key for the value to be added therefore you cannot use it to pass key⇒value item to an array in. Instead, we will use [] operator:

$user = ['name' => 'John Doe', 'email' => 'john@example.com'];
$user['type'] = 'admin';
$user['status'] = 'active';
print_r($user);

//output
[
    'name' => 'John Doe',
    'email' => 'john@example.com'
    'type' => 'admin',
    'status' => 'active'
]

Thus, you can add an item with key in associative array by pushing via []. Similarly, you can push key⇒value item to multi-dimensional array too (which makes sense tbh)

Hope, this tutorial helped you get quick understand of array_push() function as well as helped you in several use-cases of pushing items to array in PHP.

Adios,