You need to use unset
like this:
foreach($items['items'] as $index => $key) { // also get the index!
if (!isset($key['item_id'])) continue; // skip
$removeIndex = $key['item_id'] == 1;
if($removeIndex) {
unset($items['items'][$index]['item_id']); // specify path to that entry
}
}
See it run on eval.in.
To unset something in your nested array structure, you need to act on that array itself. unset($removeIndex)
does not change the array, because that is a boolean value.
The extra if
is there for the case when you don't have an item_id
in some sub-array: in that case that iteration of the loop is skipped.
Removing the entire "row"
If your aim is to also remove the sub-array to which the item_id
belongs (so including the item_amount
and any other value in that sub-array), then just shorten the "path" in the unset
statement:
foreach($items['items'] as $index => $key) { // also get the index!
if (!isset($key['item_id'])) continue; // skip
$removeIndex = $key['item_id'] == 1;
if($removeIndex) {
unset($items['items'][$index]); // specify path to that entry
}
}
See it run on eval.in.