3v4l.org

run code in 300+ PHP versions simultaneously
<?php $path = ['foo', 'bar', 'baz']; $data = true; $expanded_array = []; // Climb down the path using reference assignment (=&) // e.g. $var_ref points at $expanded_array, then $expanded_array['foo'], then $expanded_array['foo']['bar'] $var_ref =& $expanded_array; foreach ($path as $path_item) { $var_ref =& $var_ref[$path_item]; } // Set the actual value to the item we end up pointing to (note = rather than =&) $var_ref = $data; // Unset the reference, so as not to use it accidentally unset($var_ref); var_dump($expanded_array);
Output for 8.0.24, 8.1.23 - 8.1.28, 8.2.4 - 8.2.19, 8.3.0 - 8.3.7
array(1) { ["foo"]=> array(1) { ["bar"]=> array(1) { ["baz"]=> bool(true) } } }

preferences:
47.19 ms | 402 KiB | 31 Q