3v4l.org

run code in 300+ PHP versions simultaneously
<?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 ( ! empty($key_parts)) { // check if this key exists, and bail out if it doesn't if ( ! array_key_exists($key_parts[0], $target)) { return false; } // change the target $target =& $target[$key_parts[0]]; // and on to the next key part array_shift($key_parts); } // delete the target found unset($target); return true; } $arr = array('a' => array('b1' => array('c1' => 'value c1', 'c2' => 'value c2'), 'b2' => 'value b2')); var_dump(delete($arr, 'a.b.c1'), $arr);
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
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.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Parse error: syntax error, unexpected ':', expecting '{' in /in/MpdJI on line 3
Process exited with code 255.

preferences:
196.32 ms | 402 KiB | 266 Q