Deleting elements from an array in PHP

Nadia S.

The Problem

How do I remove elements from an array in PHP?

The Solution

The easiest way to delete an element from an array in PHP is by using unset().

Click to Copy
$fruits = ['apple', 'orange', 'pear']; unset($fruits[1]);

However, note that this does not re-index the array, so $fruits[2] will remain ‘pear’. We’ll go into more detail about that and explore some other options below.

Deleting a Single Element

To delete a single element from an array, you can use:

  • The array_splice() function, which removes the element based on its index and reindexes the array.
  • The unset() function, which removes the element by its key and removes the element’s index.

The array_splice() Function

The array_splice() function is the most versatile and direct solution for deleting an element from both indexed arrays and associative arrays (with key-value pairs).

We call the array_splice() function with three arguments:

  • the array,
  • the offset of the element to be removed, and
  • the number of elements you want to remove.

Given an indexed array, you can remove the element at index 0 and specify only one element to be removed:

Click to Copy
$fruits = ['apple', 'orange', 'pear']; // remove the first element and only remove one element array_splice($fruits, 0, 1); print_r($fruits);

This will output:

Click to Copy
Array ( [0] => orange [1] => pear )

The array_splice() function is a convenient method to delete a single element from an associative array, as you can remove an element based on its offset rather than its key or value. For example:

Click to Copy
$codes = [ 'red' => 'apple', 'orange' => 'orange', 'blue' => 'blueberry' ]; array_splice($codes, 0, 1); print_r($codes);

Output:

Click to Copy
Array ( [orange] => orange [blue] => blueberry )

The unset() Function

Use unset() to remove an element by its key.

Click to Copy
$fruits = ['apple', 'orange', 'pear']; // remove the second element unset($fruits[1]); print_r($fruits);

The output is:

Click to Copy
Array ( [0] => apple [2] => pear )

Removing an element from an array using the unset() function results in the element’s index being removed too. If the consecutive numerical indexing of your array is important, you can follow the unset() function with the array_values() function. The array_values() function doesn’t update the array but returns a new array, so we need to create a variable to store the value of array_values().

Here we reindex the array with array_values() so that pear is at index one:

Click to Copy
// array_values() - converts keys to numerical values $reset = array_values($fruits); print_r($reset);

The output is:

Click to Copy
Array ( [0] => apple [1] => pear )

Note that using the array_values() function with an associative array will return a new array with numeric keys, for example:

Click to Copy
$colors = [ 'red' => 'apple', 'orange' => 'orange', 'blue' => 'blueberry' ]; $colorsTest = array_values($colors); print_r($colorsTest);

The output is:

Click to Copy
Array ( [0] => apple [1] => orange [2] => blueberry )

If you want to delete an element from an array but you only know its value, you can use array_search() to find the key of the element, and then use unset() to remove the key-value pair. Note that if there are duplicate elements in the array, array_search() will only return the first match.

Click to Copy
$colors = [ 'red' => 'apple', 'orange' => 'orange', 'blue' => 'blueberry' ]; $findKey = array_search('apple', $colors); print_r($findKey);

The output is:

Click to Copy
red

Now we use unset() to remove the key-value pair from the array:

Click to Copy
unset($colors['red']); print_r($colors);

Output:

Click to Copy
Array ( [orange] => orange [blue] => blueberry )

Deleting Multiple Elements

To delete multiple nonconsecutive elements from an array, you can use:

  • The array_diff() function, which deletes elements and their indices from an indexed or associative array with the values as input.
  • The array_diff_key() function, which removes elements from an associative array using their keys as arguments.

The array_diff() Function

In this example, we call array_diff() on an indexed array to create a new array without John, Sue, and Sean:

Click to Copy
$names = ['John', 'Mary', 'Sue', 'Daniel', 'Sean']; $newNames = array_diff($names, ['John', 'Sue', 'Sean']); print_r($newNames);

The output contains the remaining elements but the indexes are no longer sequential:

Click to Copy
Array ( [1] => Mary [3] => Daniel )

As with unset(), you can use array_values() to reindex the output:

Click to Copy
Array ( [0] => Mary [1] => Daniel )

We can use array_diff() on an associative array to create a new array with elements removed based on their values. This is handy in cases where you aren’t sure of the keys of the elements to be removed.

Click to Copy
$jobs = [ 'Lawyer' => 'John', 'Teacher' => 'Mary', 'Chef' => 'Sue', 'Driver' => 'Daniel', 'Doctor' => 'Sean' ]; $newJobs = array_diff($jobs, ['John', 'Sue', 'Sean']); print_r($newJobs);

Output:

Click to Copy
Array ( [Teacher] => Mary [Driver] => Daniel )

The array_diff_key() Function

Use the array_diff_key() function when you want to delete several elements from an array using their key values.

Click to Copy
$times = [ '8:00' => 'morning', '12:00' => 'noon', '19:00' => 'night' ]; // the values can be set to an empty string, or any character or string $newTimes = array_diff_key($times, ['8:00' => '', '12:00' => '']); print_r($newTimes);

Output:

Click to Copy
Array ( [19:00] => night )

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.