3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [ 'foo.bar', 'foo.bar.baz', 'foo.bar.qux', 'foo.qux', 'foo.quux', 'bar.baz', 'bar.qux', 'qux.quux', ]; $output=[]; foreach ($input as $line){ $keys = explode('.',$line); //break each section into a key $val = true; //holds next value to add to array $localArray = []; //holds the array for this input line for($i=count($keys)-1; $i>=0; $i--){ //go in reverse order $localArray = [$keys[$i]=>$val]; //store previous value in array $val = $localArray; //store the array we just built. it will be //the value of the next key } $output = array_merge_recursive($output,$localArray); } print_r($output);

preferences:
30.26 ms | 406 KiB | 5 Q