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 at 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 add 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 add to array in PHP.

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 the array_push function.

Example 1: Adding in Numerically Indexed Array

The below code snippet contains a simple operation of the 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
	        )
	)
	*/
?>	

First, we initialize an empty array. Then we add to new elements to the array sequentially. Also, we add an array to 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 adding 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 of inserting a class object in the array.

<?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. We create three objects of the class and add them to the array. The final array contains three objects of class A.

Example 4: Shortcut to array_push

There is also a shortcut for 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.

Example 5: Array Push with Key

The array_push function in PHP can only add new elements with numeric indices. However, if you want to add a new element with a specific key, you must use the [] brackets. For instance, consider the example below:

<?php
$testArray = array();
 $testArray['key1'] = 'element1';
 $testArray['key2'] = 'element2';
 print_r($testArray);
 /*
 Array
 (
     [key1] => element1
     [key2] => element2
 )
 */
?>

Conclusion

In conclusion, we learned about using the PHP array_push function. It can be used to add 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.

Spread the Knowledge

Leave a Reply

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