3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 'Name/First' => 'John', 'Name/Last' => 'Doe', 'Age' => 20, 'Address/Postal/City' => 'New York', 'Address/Postal/Zip' => 10003, 'Address/Billing/City' => 'Los Angeles', 'Phone' => 123456789, 'Foo/Bar' => 'test1', 'Foo/Bar/Foo2/Bar2' => 'test2', ]; $newarray = []; foreach($array as $path => $value) { $keys = array_filter(explode('/', $path)); $ref = &$newarray; foreach($keys as $level => $key) { if (!array_key_exists($key, $ref)) $ref[$key] = []; $ref = &$ref[$key]; if (isset($ref) && !is_array($ref)) { break; } } if (is_array($ref)) { $ref = $value; } } print_r($newarray);

preferences:
51.53 ms | 406 KiB | 5 Q