Nadia S.
—How do I remove elements from an array in PHP?
The easiest way to delete an element from an array in PHP is by using unset()
.
$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.
To delete a single element from an array, you can use:
array_splice()
function, which removes the element based on its index and reindexes the array.unset()
function, which removes the element by its key and removes the element’s index.array_splice()
FunctionThe 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:
Given an indexed array, you can remove the element at index 0
and specify only one element to be removed:
$fruits = ['apple', 'orange', 'pear']; // remove the first element and only remove one element array_splice($fruits, 0, 1); print_r($fruits);
This will output:
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:
$codes = [ 'red' => 'apple', 'orange' => 'orange', 'blue' => 'blueberry' ]; array_splice($codes, 0, 1); print_r($codes);
Output:
Array ( [orange] => orange [blue] => blueberry )
unset()
FunctionUse unset()
to remove an element by its key.
$fruits = ['apple', 'orange', 'pear']; // remove the second element unset($fruits[1]); print_r($fruits);
The output is:
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:
// array_values() - converts keys to numerical values $reset = array_values($fruits); print_r($reset);
The output is:
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:
$colors = [ 'red' => 'apple', 'orange' => 'orange', 'blue' => 'blueberry' ]; $colorsTest = array_values($colors); print_r($colorsTest);
The output is:
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.
$colors = [ 'red' => 'apple', 'orange' => 'orange', 'blue' => 'blueberry' ]; $findKey = array_search('apple', $colors); print_r($findKey);
The output is:
red
Now we use unset()
to remove the key-value pair from the array:
unset($colors['red']); print_r($colors);
Output:
Array ( [orange] => orange [blue] => blueberry )
To delete multiple nonconsecutive elements from an array, you can use:
array_diff()
function, which deletes elements and their indices from an indexed or associative array with the values as input.array_diff_key()
function, which removes elements from an associative array using their keys as arguments.array_diff()
FunctionIn this example, we call array_diff()
on an indexed array to create a new array without John
, Sue
, and Sean
:
$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:
Array ( [1] => Mary [3] => Daniel )
As with unset()
, you can use array_values()
to reindex the output:
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.
$jobs = [ 'Lawyer' => 'John', 'Teacher' => 'Mary', 'Chef' => 'Sue', 'Driver' => 'Daniel', 'Doctor' => 'Sean' ]; $newJobs = array_diff($jobs, ['John', 'Sue', 'Sean']); print_r($newJobs);
Output:
Array ( [Teacher] => Mary [Driver] => Daniel )
array_diff_key()
FunctionUse the array_diff_key()
function when you want to delete several elements from an array using their key values.
$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:
Array ( [19:00] => night )
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.