I want to loop through an array with foreach to check if a value exists. If the value does exist, I want to delete the element which contains it.
I have the following code:
foreach($display_related_tags as $tag_name) { if($tag_name == $found_tag['name']) { // Delete element } }I don't know how to delete the element once the value is found. How do I delete it?
I have to use foreach for this problem. There are probably alternatives to foreach, and you are welcome to share them.
Kevin
52k15 gold badges97 silver badges128 bronze badges
asked Dec 22, 2009 at 21:22
1
If you also get the key, you can delete that item like this:
foreach ($display_related_tags as $key => $tag_name) { if($tag_name == $found_tag['name']) { unset($display_related_tags[$key]); } }answered Dec 22, 2009 at 21:25
GumboGumbo
628k106 gold badges767 silver badges837 bronze badges
6
A better solution is to use the array_filter function:
$display_related_tags = array_filter($display_related_tags, function($e) use($found_tag){ return $e != $found_tag['name']; });As the php documentation reads:
As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.
In PHP 7, foreach does not use the internal array pointer.
answered Aug 9, 2016 at 11:32
NeilsNeils
1,4831 gold badge12 silver badges7 bronze badges
5
foreach($display_related_tags as $key => $tag_name) { if($tag_name == $found_tag['name']) unset($display_related_tags[$key]; }
answered Dec 22, 2009 at 21:25
Steve HSteve H
9467 silver badges22 bronze badges
1
Instead of doing foreach() loop on the array, it would be faster to use array_search() to find the proper key. On small arrays, I would go with foreach for better readibility, but for bigger arrays, or often executed code, this should be a bit more optimal:
$result=array_search($unwantedValue,$array,true); if($result !== false) { unset($array[$result]); }The strict comparsion operator !== is needed, because array_search() can return 0 as the index of the $unwantedValue.
Also, the above example will remove just the first value $unwantedValue, if the $unwantedValue can occur more then once in the $array, You should use array_keys(), to find all of them:
$result=array_keys($array,$unwantedValue,true) foreach($result as $key) { unset($array[$key]); }Check //php.net/manual/en/function.array-search.php for more information.
answered Dec 22, 2009 at 22:17
PiotrNPiotrN
3101 silver badge3 bronze badges
if you have scenario in which you have to remove more then one values from the foreach array in this case you have to pass value by reference in for each: I try to explain this scenario:
foreach ($manSkuQty as $man_sku => &$man_qty) { foreach ($manufacturerSkus as $key1 => $val1) { // some processing here and unset first loops entries // here dont include again for next iterations if(some condition) unset($manSkuQty[$key1]); } } }in second loop you want to unset first loops entries dont come again in the iteration for performance purpose or else then unset from memory as well because in memory they present and will come in iterations.
answered Dec 10, 2015 at 19:24
There are already answers which are giving light on how to unset. Rather than repeating code in all your classes make function like below and use it in code whenever required. In business logic, sometimes you don't want to expose some properties. Please see below one liner call to remove
public static function removeKeysFromAssociativeArray($associativeArray, $keysToUnset) { if (empty($associativeArray) || empty($keysToUnset)) return array(); foreach ($associativeArray as $key => $arr) { if (!is_array($arr)) { continue; } foreach ($keysToUnset as $keyToUnset) { if (array_key_exists($keyToUnset, $arr)) { unset($arr[$keyToUnset]); } } $associativeArray[$key] = $arr; } return $associativeArray; }Call like:
removeKeysFromAssociativeArray($arrValues, $keysToRemove);answered Dec 26, 2017 at 12:57
Somnath MulukSomnath Muluk
52.5k34 gold badges216 silver badges224 bronze badges
1