Since you cannot invoke unset on the reference element, you need to use a different trick:
function array_unset($str, &$arr) { $nodes = split("->", $str); $prevEl = NULL; $el = &$arr; foreach ($nodes as &$node) { $prevEl = &$el; $el = &$el[$node]; } if ($prevEl !== NULL) unset($prevEl[$node]); return $arr; } $str = "baz->foo->boo"; array_unset($str, $arr);
Essentially, you go through the array tree, but keep a reference to the last array (the penultimate node) from which you want to remove the node. Then you call unset in the last array, passing the last node as the key.
Check this code
source share