Filter a value from an array in PHP
In this post, we will see how to filter a value from an array in PHP. The solution should return a new array without the specified value. Assume that the array can contain multiple items with the specified value.
1. Using array_filter() function
The standard solution to filter elements of an array is using the array_filter() function, which works by iterating over each value in the array and passing them to the callback function.
Following is a simple example demonstrating this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $arr = [1, 2, 3, 4, 3]; $val = 3; echo "The original array is " . json_encode($arr) . "\n"; $filtered = array_filter($arr, function($value) use ($val) { return $value !== $val; }); echo "The filtered array is " . json_encode($filtered) . "\n"; /* Output: The original array is [1,2,3,4,3] The filtered array is {"0":1,"1":2,"3":4} */ ?> |
As evident from the above output, the resulting array has gaps. This is because the array_filter() function preserves array keys, which results in an associative array. To resolve this, it is recommended to reindex the resultant array using the array_values() function, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $arr = [1, 2, 3, 4, 3]; $val = 3; echo "The original array is " . json_encode($arr) . "\n"; $filtered = array_values(array_filter($arr, function($value) use ($val) { return $value !== $val; })); echo "The filtered array is " . json_encode($filtered) . "\n"; /* Output: The original array is [1,2,3,4,3] The filtered array is [1,2,4] */ ?> |
For associative arrays, the code remains similar:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $arr = array("a" => "red", "b" => "blue", "c" => "green", "d" => "blue"); $val = "blue"; $filtered = array_filter($arr, function($value) use ($val) { return $value !== $val; }); print_r($filtered); /* Output: Array ( [a] => red [c] => green ) */ ?> |
2. Using array_diff() function
Another alternative is to use the array_diff() function to filter array elements by their value. It computes the difference between two arrays and return the values in array which are not present in the other array.
The idea is to convert the specified value into an array and compare it with the original array using the array_diff() function. The following code demonstrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $arr = [1, 2, 3, 4, 3]; $val = 3; echo "The original array is " . json_encode($arr) . "\n"; $filtered = array_diff($arr, [$val]); echo "The filtered array is " . json_encode($filtered) . "\n"; /* Output: The original array is [1,2,3,4,3] The filtered array is {"0":1,"1":2,"3":4} */ ?> |
Note that the array_diff() function results in an associative array. To resolve this, reindex the resultant array using the array_values() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $arr = [1, 2, 3, 4, 3]; $val = 3; echo "The original array is " . json_encode($arr) . "\n"; $filtered = array_values(array_diff($arr, [$val])); echo "The filtered array is " . json_encode($filtered) . "\n"; /* Output: The original array is [1,2,3,4,3] The filtered array is [1,2,4] */ ?> |
Here’s an example using the associative arrays.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $arr = array("a" => "red", "b" => "blue", "c" => "green", "d" => "blue"); $val = "blue"; $filtered = array_diff($arr, [$val]); echo json_encode($filtered); /* Output: Array ( [a] => red [c] => green ) */ ?> |
Finally, if you need to filter an array using keys, you can use the array_diff_key() function. Here’s a working example:
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $arr = array("r" => "red", "b" => "blue", "g" => "green", "y" => "yellow"); $keys = ["r", "g"]; $filtered = array_diff_key($arr, array_flip($keys)); echo json_encode($filtered); /* Output: {"b":"blue","y":"yellow"} */ ?> |
That’s all about filtering a value from an array in PHP. Refer this post post to in-place remove a specific element from an array by its value in PHP.
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 :)