代码之家  ›  专栏  ›  技术社区  ›  sagar27

基于URL生成父子数组

  •  -1
  • sagar27  · 技术社区  · 14 年前

    我有以下url/dir1/dir2/page.php

    我需要从上面的url生成父-子关系的数组。

    /dir1
        |
        |
        /dir2/
            |
            |
            page.php
    

    如:

    array('dir1'=> array(
            'child' => array( 'dir2' => array(
                        'child' => array( 
                                'page' => array())))));
    

    我试过使用递归函数,但没能找出逻辑。 如果你需要更多的解释,请告诉我。

    3 回复  |  直到 14 年前
        1
  •  0
  •   Matt Williamson    14 年前

    这里有一个递归方法(很抱歉没有注释,很晚了):

    <?php
    $urls = array('/dir1/dir2/page.php', '/dir1/dir3/page');
    
    function url_to_array($url, $array=null) {
        if($array === null) {
            $array = array();
            $url_parts = explode('/', $url);
    
            return url_to_array($url_parts, $array);
        } else {
            if(count($url)) {
                $item = array_pop($url);
                if($item) {
                    if(!$array) {
                        $array = array('page' => $array);
                    } else {
                        $array = array($item => array('child' => $array));
                    }
                }
                return url_to_array($url, $array);
            } else {
                return $array;
            }
        }
    }
    
    $array = array();
    foreach($urls as $url) {
        $array = array_merge_recursive($array, url_to_array($url));
    }
    
    print_r($array);
    ?>
    

    它打印出来:

    wraith:Downloads mwilliamson$ php recursive.php 
    Array
    (
        [dir1] => Array
            (
                [child] => Array
                    (
                        [dir2] => Array
                            (
                                [child] => Array
                                    (
                                        [page] => Array
                                            (
                                            )
    
                                    )
    
                            )
    
                        [dir3] => Array
                            (
                                [child] => Array
                                    (
                                        [page] => Array
                                            (
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
        2
  •  0
  •   Blender    14 年前

    这是一个论坛的拷贝/粘贴,所以我不知道它是否有效(我浏览了一下,它看起来合法)。

    function getDirectory($path = '.', $ignore = '')
    { 
      $dirTree = array();
      $dirTreeTemp = array();
      $ignore[] = '.'; 
      $ignore[] = '..'; 
    
      $dh = opendir($path); 
    
      while (false !== ($file = readdir($dh)))
      {
        if (!in_array($file, $ignore))
        {
          if (!is_dir("$path/$file"))
          {
            $dirTree["$path"][] = $file;
          } else {
            $dirTreeTemp = getDirectory("$path/$file", $ignore);
    
            if (is_array($dirTreeTemp))
            {
              $dirTree = array_merge($dirTree, $dirTreeTemp);
            }
          } 
        } 
      }
    
      closedir($dh);
    
      return $dirTree;
    }
    
        3
  •  0
  •   Alec Gorge    14 年前

    这是我为这个问题专门写的一个实际的解决方案。不涉及递归!

    这将返回与您在问题中发布的数组完全相同的数组。

    function recursiveArrayFromURL () {
        $parts = array_slice(explode("/", $_SERVER['REQUEST_URI']), 1);
        $refs = array();
        foreach($parts as $key => $v) {
            // remove extension from last file
            if($key == count($parts) - 1) $v = substr($v, 0, -4);
    
            $thisref = &$refs[$v];
            if(is_null($thisref)) $thisref = array();
    
            $parent = end(array_slice($parts, 0, $key - 1));
    
            if(!array_key_exists("child", $refs[$parent])) $refs[$parent]['child'] = array();
            $refs[$parent]['child'][ $v ] = $thisref; 
        }
        return array_slice($refs, 0, 1);
    }