I have a multidimensional array $sub_objects
. Currently the script below unsets the keys that have 'Apples' included. Instead I want to do the opposite. I want to unset keys that DO NOT have the value 'Apples'. I tried setting if(strpos($value, 'Apples') !== false)
but that did not do anything. How can I unset values that do not have 'Apple' instead of those that do? As you can see only I like Green Eggs and Ham
is left in the output, but this is the only one that should be unset. The first 3 should remain, but the 4th should be unset/deleted.
Thanks!
$sub_objects = [
['text' => 'I like Apples', 'id' => '102923'],
['text' => 'I like Apples and Bread', 'id' =>'283923'],
['text' => 'I like Apples, Bread, and Cheese', 'id' => '3384823'],
['text' => 'I like Green Eggs and Ham', 'id' =>'4473873']
];
foreach($sub_objects as $key => $array) {
foreach($array as $value) {
if(strpos($value, 'Apples') !== false) {
//print_r($key);
unset($sub_objects[$key]);
}
}
}
print_r($sub_objects);
output:
Array
(
[3] => Array
(
[text] => I like Green Eggs and Ham
[id] => 4473873
)
)