3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function delete(array &$array, string $key): bool { // process the parts $target =& $array; // split the key in it's parts $key_parts = explode('.', $key); while (true) { // check if this key exists, and bail out if it doesn't if ( ! array_key_exists($key_parts[0], $target)) { return false; } // last key component? if (count($key_parts) === 1) { // delete the target found unset($target[$key_parts[0]]); return true; } // change the target $target =& $target[$key_parts[0]]; // and on to the next key part array_shift($key_parts); } } $arr = array('a' => array('b1' => array('c1' => 'value c1', 'c2' => 'value c2'), 'b2' => 'value b2')); var_dump(delete($arr, 'a.b.c1'), $arr);
based on MpdJI
Output for 7.0.0 - 7.2.0
bool(false) array(1) { ["a"]=> array(2) { ["b1"]=> array(2) { ["c1"]=> string(8) "value c1" ["c2"]=> string(8) "value c2" } ["b2"]=> string(8) "value b2" } }
Output for 5.3.19 - 5.6.28
Parse error: syntax error, unexpected ':', expecting '{' in /in/2TruM on line 3
Process exited with code 255.