Remove a specific element from an array in PHP
In this post, we will see how to remove a specific element from an array by its value in PHP.
1. Using array_search() function
If the array contains only a single element with the specified value, you can use the array_search() function with unset() function to remove it from an array. The array_search() function searches the array for a given value and returns the first corresponding key if present in the array, false otherwise. If the value is found in the array, you can remove it using the unset() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $arr = array("a" => "red", "b" => "blue", "c" => "green"); $val = "blue"; $key = array_search($val, $arr, true); if ($key !== false) { unset($arr[$key]); } echo json_encode($arr); /* Output: {"a":"red","c":"green"} */ ?> |
For the plain arrays, the code remains similar:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $arr = [1, 2, 3, 4, 5]; $val = 3; // To perform a strict type comparison, pass the third parameter as true $key = array_search($val, $arr, true); if ($key !== false) { unset($arr[$key]); } echo json_encode($arr); /* Output: {"0":1,"1":2,"3":4,"4":5} */ ?> |
Note that the resulting array has holes since the numerical keys in the array are preserved. You can replace the unset() call with the array_splice() function, which removes a portion of the array and doesn’t preserve numerical keys.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $arr = [1, 2, 3, 4, 5]; $val = 3; $key = array_search($val, $arr, true); if ($key !== false) { array_splice($arr, $key, 1); } echo json_encode($arr); /* Output: [1,2,4,5] */ ?> |
2. Using foreach construct
If your array can contain multiple elements with the specified value, you can use a simple foreach loop to iterate over the array, and unset all matching values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $arr = array("a" => "red", "b" => "blue", "c" => "green", "d" => "blue"); $val = "blue"; foreach ($arr as $key => $value) { if ($value == $val) { unset($arr[$key]); } } echo json_encode($arr); /* Output: {"a":"red","c":"green"} */ ?> AVITyM |
Note that in each iteration of the loop, the value of the current element is compared with the given value, and it unsets the matching items. The code can be shortened using the array_keys() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $arr = array("a" => "red", "b" => "blue", "c" => "green", "d" => "blue"); $val = "blue"; foreach (array_keys($arr, $val) as $key) { unset($arr[$key]); } echo json_encode($arr); /* Output: {"a":"red","c":"green"} */ ?> |
Here’s an example using plain arrays:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $arr = [1, 2, 3, 4, 3, 5]; $val = 3; foreach ($arr as $key => $value) { if ($value == $val) { //unset($arr[$key]); array_splice($arr, $key, 1); } } echo json_encode($arr); /* Output: {"0":1,"1":2,"3":4,"5":5} */ ?> |
That’s all about removing a specific element from an array in PHP.
Related Posts:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)