Be careful with the main answer.
with
[['id'=>1,'cat'=>'vip'] ,['id'=>2,'cat'=>'vip'] ,['id'=>3,'cat'=>'normal']and calling the function
foreach($array as $elementKey => $element) { foreach($element as $valueKey => $value) { if($valueKey == 'cat' && $value == 'vip'){ //delete this particular object from the $array unset($array[$elementKey]); } } }it returns
[2=>['id'=>3,'cat'=>'normal']instead of
[0=>['id'=>3,'cat'=>'normal']It is because unset does not re-index the array.
It reindexes. (if we need it)
$result=[]; foreach($array as $elementKey => $element) { foreach($element as $valueKey => $value) { $found=false; if($valueKey === 'cat' && $value === 'vip'){ $found=true; $break; } if(!$found) { $result[]=$element; } } }View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Use unset() function to remove array elements in a foreach loop. The unset() function is an inbuilt function in PHP which is used to unset a specified variable. The behavior of this function depends on different things. If the function is called from inside of any user defined function then it unsets the value associated with the variables inside it, leaving the value which is initialized outside it.
Syntax:
unset( $variable )Return Value: This function does not returns any value.
Below examples use unset() function to remove an array element in foreach loop.
Example 1:
<?php
$Array = array(
"GeeksForGeeks_1",
"GeeksForGeeks_2",
"GeeksForGeeks_3"
);
print_r($Array);
foreach($Array as $k => $val) {
if($val == "GeeksForGeeks_3") {
unset($Array[$k]);
}
}
print_r($Array);
?>
Output:
Array ( [0] => GeeksForGeeks_1 [1] => GeeksForGeeks_2 [2] => GeeksForGeeks_3 ) Array ( [0] => GeeksForGeeks_1 [1] => GeeksForGeeks_2 )Example 2:
<?php
$Array = array(
array(0 => 1),
array(4 => 10),
array(6 => 100)
);
print_r($Array);
foreach($Array as $k => $val) {
if(key($val) > 5) {
unset($Array[$k]);
}
}
print_r($Array);
?>
Output:
Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [4] => 10 ) [2] => Array ( [6] => 100 ) ) Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [4] => 10 ) )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.
<?php $arr=array("a"=>"red","b"=>"blue", "c"=>"green"); $val ="blue"; $key= array_search($val,$arr,true); if($key!==false){ unset($arr[$key]); } echojson_encode($arr); /* Output: {"a":"red","c":"green"} */ ?> |
For the plain arrays, the code remains similar:
<?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]); } echojson_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.
<?php $arr=[1, 2,3,4,5]; $val=3; $key= array_search($val,$arr,true); if($key!==false){ array_splice($arr,$key,1); } echojson_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($arras$key=> $value){ if($value ==$val){ unset($arr[$key]); } } echojson_encode($arr); /* Output: {"a":"red","c":"green"} */ ?> |
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:
<?php $arr=array("a"=>"red","b"=>"blue", "c"=>"green","d"=> "blue"); $val="blue"; foreach(array_keys($arr,$val)as$key){ unset($arr[$key]); } echojson_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($arras$key=> $value){ if($value ==$val){ //unset($arr[$key]); array_splice($arr,$key,1); } } echojson_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:
Filter a value from an array in PHP
Thanks for reading.
Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and help us grow. Happy coding 🙂