How to Remove Elements from Arrays in PHP

On this page we demonstrate and describe the various ways that you can remove elements from arrays in PHP. We cover the array_pop, array_shift, and unset functions. The array_splice function, which can also be used to remove array elements, is discussed elsewhere.

unset

PHP's unset function can be used to remove individual elements from an array. The following demonstrates using unset to remove the third element from an exmple array:

$ar = array('Morie', 'Miki', 'Coco', 'Halo');
unset( $ar[2] ); // unset 3rd element
print_r($ar);
/* print_r output (as seen in page source view): 
Array
(
    [0] => Morie
    [1] => Miki
    [3] => Halo
) */

Notice that the unset function leaves a gap in our numerically indexed array. We can use the array_values function to return a re-indexed copy:

// pass $ar to array_values
$ar2 = array_values($ar);
// display array returned by array_values
print_r($ar2);
/* Array
(
    [0] => Morie
    [1] => Miki
    [2] => Halo
) */

Note that while the array returned by the array_values function is re-indexed, the array passed to it is not affected.

We can use the unset function to remove a key/value pair from an associative array, as the following demonstrates:

$person = array(
    'name' => 'Jon',
    'age' => 26,
    'friends' => array('Matt', 'Kaci', 'Jess')
);

unset( $person['friends'] );
print_r($person);
/* Array
(
    [name] => Jon
    [age] => 26
) */

array_pop

The array_pop function removes the last element from the array passed to it. It returns that removed element and reduces the length of the array by one. The following demonstrates:

$ar = array('Morie', 'Miki', 'Coco', 'Halo');
// pass $ar to array_pop
$el = array_pop($ar);
// inspect array_pop return value
var_dump($el); // string(4) "Halo"
// view modified $ar
print_r($ar);
/* Array
(
    [0] => Morie
    [1] => Miki
    [2] => Coco
) */
echo count($ar); // 3 ($ar now has 3 elements)

The array_pop function can be applied to associative arrays as well as numerically indexed ones:

$person = array(
    'name' => 'Jon',
    'age' => 26,
    'friends' => array('Matt', 'Kaci', 'Jess')
);
// pass $person to array_pop
$el = array_pop($person);
// inspect array_pop return value
var_dump($el);
/* array(3) {
  [0]=>
  string(4) "Matt"
  [1]=>
  string(4) "Kaci"
  [2]=>
  string(4) "Jess"
} */

// view modified $person
print_r($person);
/* Array
(
    [name] => Jon
    [age] => 26
) */

array_shift

The array_shift function is similar to array_pop except that instead of removing the last element, it removes the first. The array_shift function returns the removed element and reduces the length of the array passed to it by one. The following demonstrates:

$ar = array('Morie', 'Miki', 'Coco', 'Halo');
$el = array_shift($ar);
var_dump($el); // returned by array_shift: string(5) "Morie"
print_r($ar);// view modified $ar
/* Array
(
    [0] => Miki
    [1] => Coco
    [2] => Halo
) */
echo count($ar); // 3 (resulting length of $ar)

The array_shift function re-indexes arrays with numeric keys, starting from zero. Like array_pop, the array_shift function can also be applied to associative arrays.

array_splice

The array_splice function can be used to remove elements anywhere in an array. Find out more.

Back to top