Remove an Element From an Array by Value in PHP

There are different methods in PHP to remove an element from an array by value.

The most simple method is the combination of array_search() and unset() functions. So let’s check each method one by one.

Method 1 : Using array_search() and unset()

Suppose we have an array i.e. keywords. Now we want to remove the element ‘array from it.

<?php

//list of array elements	
$keywords = array("remove", "array", "element", "by", "value");

//search a specific value i.e. "array" and store the index into $key
$key = array_search("array", $keywords);

//check if index value present
if ( $key !== false) {
	
	//remove the specific index from array
    unset($keywords[$key]);
}

var_dump($keywords);

Output:

array (size=4)
  0 => string 'remove' (length=6)
  2 => string 'element' (length=7)
  3 => string 'by' (length=2)
  4 => string 'value' (length=5)

So, array_search() will return the index. In this case, the index of the element ‘array’ is 1. Now we are checking whether the value of the $key is false. If not, remove the element from that array.

We are using the strict comparison operator i.e. !== here. Because if the index of that particular element is 0, then comparison can be problematic. That’s why we are using !==.

The unset() does not re-index the array. So we need to re-index it by using array_values().

Method 2: Using array_filter() and arrow functions

If you are using PHP 7.4, you can use array_filter() with arrow functions.

<?php
//list of array elements	
$keywords = array("remove", "array", "element", "by", "value");

$keywords = array_filter($keywords, fn ($m) => $m != "array");

var_dump($keywords);

Output:

array (size=4)
  0 => string 'remove' (length=6)
  2 => string 'element' (length=7)
  3 => string 'by' (length=2)
  4 => string 'value' (length=5)

array_filter() iterates over each value in an array and pass the value to the callback function. Here we have used arrow functions as a callback. If the condition matched i.e. $m != "array", then it will return true.

That means if the condition is not matched, it will not be returned to the result array.

But one thing to keep in mind is that unset() can only remove the first occurrence. Suppose we have a duplicate value in our array, the first method only removes the first occurrence. But in this method, we can remove all the element which has the same value.

Method 3: Deleting an element using array_diff()

By using array_diff() you can easily remove multiple items at a time. But it will create a new array without changing in the original array.

<?php

//list of array elements	
$keywords = array("remove", "array", "element", "by", "value");

$keywords = array_diff( $keywords, ["remove", "by", "value"] );

var_dump($keywords);

Output:

array (size=2)
  1 => string 'array' (length=5)
  2 => string 'element' (length=7)

Method 4: Using array_keys()

The array_keys() will return all the keys of an array. Here we want the key of a specific value, so we have to specify the search value. Otherwise, it will return all the keys from that array.

<?php

//list of array elements	
$keywords = array("remove", "array", "element", "by", "value");

foreach (array_keys($keywords, "by", true) as $key) {
    unset($keywords[$key]);
}

var_dump($keywords);

Output:

array (size=4)
  0 => string 'remove' (length=6)
  1 => string 'array' (length=5)
  2 => string 'element' (length=7)
  4 => string 'value' (length=5)

This method can remove multiple occurrences of a specific term inside an array.

Conclusion

I think the above methods will help you to remove an element from an array by value. That’s all for today’s tutorial. You can also learn something new in PHP from our PHP Tutorial. If you have any questions please comments below.

About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment