I am trying to use unset in a foreach loop, but it is not working.
My code:
$aggr = $_GET;
foreach($aggr as $key => $value)
{
$pos_key = preg_replace('/dst_addr/', '', $key);
// why this not works:
unset($aggr[$key]);
unset($aggr[$key.'_h'.$pos_key]);
}
In the second iteration my key is eq $key.'_h'.$pos_key, but this key should be deleted and isn’t.
PHP makes a copy of the array to iterate over it. Since you end up with an empty array anyway, use a stack: