PHP array_push Function | Insert elements In Array

PHP array_push Function is an inbuilt function in PHP which inserts new elements in an array. It always inserts elements at the end of the array. The count of the array is also incremented by one. Moreover, multiple elements can be passed in the array_push function at once. In this article, we will discuss the PHP array_push Function. Also, we will discuss a few examples of using it.

Note: PHP array_unshift Function adds elements in the beginning of the array.

Syntax

array_push($array, $val1, $val2, $val3 ….);

Parameters

We can pass multiple parameters in the PHP array_push Function. It depends on the number of elements we want to push at once. Let’s consider two types of categories of elements that can be passed:

  1. $array: The first parameter is a mandatory array. The PHP array_push Function inserts new elements in this input array. An empty array can also be passed as the first parameter.
  2. $elementList: The second parameter is the comma-separated element list we want to push in the array.

Return Value

The function returns the modified array with all the elements inserted at the end.

Note: The PHP array_push method always adds a new numeric key in the original array even if the array has string keys.

Examples

Let’s discuss a few examples of using array_push function.

Example 1: Simple Operation

The below code snippet contains simple operation of array_push function.

<?php
	
	$testArray = array();
	
	array_push($testArray, 1);
	print_r($testArray);
	/*
	Array
	(
	    [0] => 1
	)
	*/

	array_push($testArray, 'Concatly');
	print_r($testArray);
	/*
	Array
	(
	    [0] => 1
	    [1] => Concatly
	)
	*/

	array_push($testArray, array('John', 'Doe'));
	print_r($testArray);
	/*
	Array
	(
	    [0] => 1
	    [1] => Concatly
	    [2] => Array
	        (
	            [0] => John
	            [1] => Doe
	        )
	)
	*/
?>	

An array is initialized in the above example. Firstly, I pushed an integer value in the array. Secondly, I pushed a single string in the array. At the last, an array is pushed in the original array. It can be clearly observed in the output that new array elements are always pushed at the end of the original array.

Example 2: Associative Arrays

For instance, let’s go through an example with an associative array.

<?php
         $testArray = array(
		'Chandler' => array(
			'name' => 'Chandler',
			'age' => 24
		),
		'Monica' => array(
			'name' => 'Monica',
			'age' => 23
		)
	);
	$ross = array(
		'name' => 'Ross',
		'age' => 25
	);
	$rachael = array(
		'name' => 'Rachael',
		'age' => 24
	);
	array_push($testArray, $ross, $rachael);
	print_r($testArray);
?>
Output         
Array
(
[Chandler] => Array
(
[name] => Chandler
[age] => 24
)
[Monica] => Array
(
[name] => Monica
[age] => 23
)
[0] => Array
(
[name] => Ross
[age] => 25
)
[1] => Array
(
[name] => Rachael
[age] => 24
)
)

In the above example, we have an associative array. Two new arrays are initialized containing some key-value pairs. However, on pushing the two new arrays in the original array, new numeric keys are created at the end. This clearly shows that the PHP array_push function always inserts numeric keys even if the array has string keys.

Example 3: Using Class Objects

We can push any data type in an array. For instance, consider the below example:

<?php

	class A {

		private $a;

		public function __construct($a) {
			$this->a = $a;
		}
		public function getA() {
			return $this->a;
		}
	}

	$objectArray = array();
	$a1 = new A(10);
	$a2 = new A(20);
	$a3 = new A(30);
	array_push($objectArray, $a1, $a2, $a3);
	print_r($objectArray);
?>
Output
Array
(
[0] => A Object
(
[a:A:private] => 10
)
[1] => A Object
(
[a:A:private] => 20
)
[2] => A Object
(
[a:A:private] => 30
)
)

In the above example, we have a class A. Three objects of the class A are created and pushed in an empty array. The final array contains three objects of class A.

Example 4: Shortcut to array_push

There is also a shortcut of using the PHP array_push Function. However, this method can only insert one key at a time. For example, go through the following code snippet:

<?php
	$testArray = array(1,2,3);
	$testArray[] = 4;
	$testArray[] = 5;
	$testArray[] = 6;
	print_r($testArray);
?>
Output

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)

Instead of using the array_push function, we can perform the same task by using a blank square brace with the array. It also inserts the new elements at the end with numeric keys.

Conclusion

In conclusion, we learned about using the PHP array_push function. It can be used to insert new elements at the end of the array. You may go through the official documentation of array_push function. Additionally, you may also read about other PHP array functions here.

You may also checkout this very informative article on various ways to Add to Array in PHP.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *