PHP Array Push Pop Shift and Unshift

Overview

Many if not all PHP developers will come across arrays in their day to day programming. Arrays allow you to store ‘rows’ or ‘blocks’ of information. PHP comes with plenty functions that can be used to create, manage and simplify the use of arrays. I will focus on four functions that allow you to add and remove elements from an array. I will also show you how to access the first and last elements of an array using these functions.

PHP Array Functions

PHP Array Push Pop Shift and Unshift

For each of the four functions, I will give a short overview of what each one is used for as well as alternative functions that can be used to achieve the same result. Remember that each of the four functions will reset the internal pointer for the array when used causing your ‘foreach’ function to start at the beginning of the array.

PHP Push Array – array_push()

Add one or more elements onto the end of array. Returns the number of elements in the new array.

Standard method using array_push();

// create our array with 1 element
$arr = array("one");
// $count will be 3 and $arr will now be array("one","two","three");
$count = array_push($arr,"two","three");

Alternate method using $arr[]

tip! Using this method can be faster than using array_push since there is no overhead of calling an actual function.

// create our array with 1 element
$arr = array("one");
// $arr will now be array("one","two");
$arr[] = "two";
// $arr will now be array("one","two","three");
$arr[] = "three";
// $count will be 3
$count = count($arr);

Alternate method using array_merge();

// create our array with 1 element
$arr = array("one");
// alternate method using array_merge()
$arr = array_merge($arr,array("two","three")); // $arr will now be array("one","two","three");
$count = count($arr); // $count will be 3

PHP Pop Array – array_pop()

Removes and returns the value of the last element of an array. Returns NULL if array is empty or not set.

Standard method using array_pop();

// create our array with 3 elements
$arr = array("one","two","three");
// $value will be "three" and array's value will now be array("one","two");
$value = array_pop($arr);

Alternate method using array_slice();

// create our array with 3 elements
$arr = array("one","two","three");
// alternative method using array_slice()
$value = $arr[count($arr)-1]; // $value will no be "three"
$arr = array_slice($arr, 0, -1);   // $arr will now be array("one","two");

PHP Shift Array – array_shift()

Removes and returns the value of the first element of an array. Returns NULL if array is empty or not set.

Standard method using array_shift();

// create our array with 3 elements
$arr = array("one","two","three");
// $value will be "one" and array's value will now be array("two","three");
$value = array_shift($arr);

Alternate method using array_slice();

// create our array with 3 elements
$arr = array("one","two","three");
$value = $arr[0]; // $value will now be "one"
$arr = array_slice($arr, 1); // array's value will now be array("two","three");

PHP Unshift Array – array_unshift()

Add one or more elements to the beginning of an array. Returns the number of elements in the new array.

Standard method using array_unshift();

// create our array with 3 elements
$arr = array("three","four","five");
// $count will now be 5 and array will hold one - five
$count = array_unshift($arr,"one","two");

Alternate method using array_slice();

// create our array with 3 elements
$arr = array("three","four","five");
// $count will now be 5 and array will hold one - five
$arr = array_merge(array("one","two"),$arr);
$count = count($arr);