PHP Remove Elements from Array: 5 Key & Value Code Examples

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

How to Remove Elements From PHP Array

This article explores different options to delete an element from an array. PHP has many functions helpful in deleting elements from an array. As we have many options to explore so let’s jump straight to these without wasting any time.

PHP Remove from Array by Value PHP Code Example

<?php
$names = ['Micheal','Andrei','Susi','Ron','Brian', 'Arthur', 'Bob'];
 
 
unset($names[0]); //Removes the name Micheal from the array. Zero indicates the index of Micheal in the array.
 
$names = array_values($names); //Just for reindexing the array.
 
/*
Array
(
    [0] => Andrei
    [1] => Susi
    [2] => Ron
    [3] => Brian
    [4] => Arthur
    [5] => Bob
)
*/
?>

How to Remove from PHP array By Key

PHP unset function destroys a specified variable or an array element. 

unset(mixed $var, mixed ...$vars): void

The function gets one or more arguments and destroys them all at once. So, this function is super handy. The following example demonstrates that.

PHP Remove from Array By Key PHP Code Example

<?php
$names = ['Micheal','Andrei','Susi','Ron','Brian', 'Arthur', 'Bob'];
 
 
unset($names[0]); //Removes the name Micheal from the array. Zero indicates the index of Micheal in the array.
 
/*
Array
(
    [1] => Andrei
    [2] => Susi
    [3] => Ron
    [4] => Brian
    [5] => Arthur
    [6] => Bob
)
*/
?>

The unset function deletes the first element ‘Micheal’. However, it doesn’t reindex the array. Unfortunately, there is no way that this function could do reindexing though there’s a workaround it costs an overhead of an extra function call – array_values.

<?php
$names = ['Micheal','Andrei','Susi','Ron','Brian', 'Arthur', 'Bob'];
 
 
unset($names[0]); //Removes the name Micheal from the array. Zero indicates the index of Micheal in the array.
 
$names = array_values($names); //Just for reindexing the array.
 
/*
Array
(
    [0] => Andrei
    [1] => Susi
    [2] => Ron
    [3] => Brian
    [4] => Arthur
    [5] => Bob
)
*/
?>

Moreover, the key is hardcoded for the value ‘Micheal’. There is a way out for that as the following example shows.

<?php
$names = ['Micheal','Andrei', 'Ron', 'Susi','Ron','Ron','Brian', 'Arthur', 'Bob', 'Ron'];
 
 
foreach(array_keys($names, 'Ron') as $key)
{
    unset($names[$key]);
}
 
/*
Array
(
    [0] => Micheal
    [1] => Andrei
    [3] => Susi
    [6] => Brian
    [7] => Arthur
    [8] => Bob
)
*/
 
?>

The name ‘Ron’ is duplicated a few times but the example uses array_keys with the search argument. The function with search argument returns an array of keys that have the value ‘Ron’.

So, the unset function does remove array elements but it is yet not the best solution. Luckily, there’s a function that saves us all the hassle. So, let’s see that next.

Delete an element from an array with array_diff

PHP array_diff computes the differences of arrays. The example here will make it clear. 

array_diff(array $array, array ...$arrays): array

The function computes the difference $array with the rest of the $arrays. It works like the difference operation from set theory in mathematics – high school stuff, remember?

PHP array_diff

So, let’s see how array_diff helps to delete an element from an array.

<?php
$names = ['Micheal','Andrei','Susi','Ron','Brian', 'Arthur', 'Bob', 'Ron', 'Ron'];
 
$names = array_diff($names, ['Ron', 'Susi']);
 
/*
Array
(
    [0] => Micheal
    [1] => Andrei
    [4] => Brian
    [5] => Arthur
    [6] => Bob
)
*/
?>

Voila! The function does all the heavy lifting. We do not need any explicit loops to delete duplicate elements nor do we have an extra function call for reindexing the array.

Delete an element from an array with array_diff_key

Now as we have learned about array_diff, it is the right time to see array_diff_key as well. The array_diff_key does the same function as array_diff except that it does the matching on keys rather than values.

Let’s just revisit the last example but invert the key and value pairs in the array.

<?php
$names = ['Micheal'=> 0,'Andrei' => 1,'Susi' => 2,'Ron' => 3, "Ron" => 4];
 
$names = array_diff($names, ['Ron'=>100]);
 
print_r($names);
 
/*
Array
(
    [Micheal] => 42
    [Andrei] => 22
    [Susi] => 18
)
*/
?>

So, the function deletes the values with the key “Ron”. The key matters here, not the value.

Delete an element from an array with array_diff_assoc

PHP array_diif_assoc is yet another similar function with the difference that it matches both – keys and values. 

Let’s just see an example with array_diff_assoc to see the difference.

<?php
$names = ['Micheal','Andrei','Susi','Ron', 'Micheal'];
 
$names = array_diff_assoc($names, ['Micheal']);
 
print_r($names);
 
/*
Array
(
    [1] => Andrei
    [2] => Susi
    [3] => Ron
    [4] => Micheal
)
*/
?>

So, the function deletes the value ‘Micheal’ with key 0 and retains the value ‘Micheal’ with key 4. That’s because the second argument gets an array that includes ‘Micheal’ with key 1. So the matching happens as, ‘delete the value Micheal at key 0’.

The array_diff family has another function array_diff_uassoc but we won’t be covering it because we already have detoured slightly from the main course.

Delete an element from an array with array_splice

PHP array_splice is another function that helps to delete an element from an array and optionally replace it with some other element. 

array_splice( array &$array, int $offset, ?int $length = null, mixed $replacement = [] ) : array

The offset and length argument is all we need to know for deletion as we don’t intend to replace it. However, understanding the function overall could be a hassle for starters. 

FuelingPHP has an in-depth article about PHP array_splice. It includes clear explanations and visuals to help in understanding the function based on the different combinations of offset and length values.

The following example uses this function to delete an element from an array. 

<?php
$names = ['Micheal','Andrei','Susi','Ron', 'Dutch'];
 
array_splice($names, 1 , 1);
 
print_r($names);
 
/*
Array
(
    [0] => Micheal
    [1] => Susi
    [2] => Ron
    [3] => Dutch
)
*/
?>

Perfect! It deletes the element ‘Andrei’ from the array. Notice that it also reindexes the array.

Conclusion

That’s pretty much all about this article. The article includes a variety of options to delete an element from an array. The unset function works fine but couldn’t reindex the array. The array_diff function is super helpful as it reindexes the array and also deletes the duplicate values. 

The article also includes examples of more functions similar to array_diff – array_diff_key and array_diff_assoc. Finally, we have the array_splice function that needs an offset and length value to delete an element or a portion of an array.

Hopefully, this article has been helpful to you. If you want to see more articles and tutorials about PHP, stay tuned at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.