3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $deep = ['a' => ['b' => ['c' => 5]]]; function set(array $array, array $keys, $value) { while ($key = array_pop($keys)) { $value = [$key => $value]; } return array_replace_recursive($array, $value); } function get(array $array, array $keys, $default = null) { while ($key = array_shift($keys)) { if (!isset($array[$key])) { return $default; } $array = $array[$key]; } return $array; } $test = set($deep, ['a', 'b', 'c'], 15); $value = get($test, ['a', 'b', 'c'], 'spaghetti'); var_dump($deep); var_dump($test); var_dump($value);
based on Wn5UV
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.3.0alpha1
array(1) { ["a"]=> array(1) { ["b"]=> array(1) { ["c"]=> int(5) } } } array(1) { ["a"]=> array(1) { ["b"]=> array(1) { ["c"]=> int(15) } } } int(15)