fork(4) download
  1. <?php
  2.  
  3. $a = ["I", "need", "this", "to", "be", "nested"];
  4.  
  5. $result = array_reduce(array_reverse($a), function($prevArray, $key){
  6. return $prevArray ? [$key => $prevArray] : [$key];
  7. }, null);
  8.  
  9. print_r($result);
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
Array
(
    [I] => Array
        (
            [need] => Array
                (
                    [this] => Array
                        (
                            [to] => Array
                                (
                                    [be] => Array
                                        (
                                            [0] => nested
                                        )

                                )

                        )

                )

        )

)
array(1) {
  ["I"]=>
  array(1) {
    ["need"]=>
    array(1) {
      ["this"]=>
      array(1) {
        ["to"]=>
        array(1) {
          ["be"]=>
          array(1) {
            [0]=>
            string(6) "nested"
          }
        }
      }
    }
  }
}