Variable containing a path as a string to multi-dimensional array?

前端 未结 1 1811
清酒与你
清酒与你 2020-12-10 22:45

I\'m looking to take a string such as

\"/test/uri/to/heaven\"

and turn it into a multi-dimensional, nested array such as:

a         


        
相关标签:
1条回答
  • 2020-12-10 23:06

    Here is a quick non recursive hack:

    $url   = "/test/uri/to/heaven";
    $parts = explode('/',$url);
    
    $arr = array();
    while ($bottom = array_pop($parts)) {        
        $arr = array($bottom => $arr);
    }
    
    var_dump($arr);
    

    Output:

    array(1) {
      ["test"]=>
      array(1) {
        ["uri"]=>
        array(1) {
          ["to"]=>
          array(1) {
            ["heaven"]=>
            array(0) {
            }
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题