根据URL生成父子数组

发布于 2024-10-03 22:58:02 字数 410 浏览 2 评论 0原文

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

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

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

例如:

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

我尝试使用递归函数但无法弄清楚逻辑。 如果您需要更多解释,请告诉我。

I have the following url /dir1/dir2/page.php

I need to generate a an array of the parent->child relation form the above url.

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

eg :

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

I tried using the recursive function but not able to figure out the logic.
Please let me know if you need some more explanation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惟欲睡 2024-10-10 22:58:02

这是一个递归方法(抱歉缺少注释,已经晚了):

<?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
                                        (
                                        )

                                )

                        )

                )

        )

)

Here's a recursive method (sorry for lack of comments, it's late):

<?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);
?>

It prints out:

wraith:Downloads mwilliamson$ php recursive.php 
Array
(
    [dir1] => Array
        (
            [child] => Array
                (
                    [dir2] => Array
                        (
                            [child] => Array
                                (
                                    [page] => Array
                                        (
                                        )

                                )

                        )

                    [dir3] => Array
                        (
                            [child] => Array
                                (
                                    [page] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)
彼岸花似海 2024-10-10 22:58:02

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

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;
}

Here's a copy/paste from a forum, so I don't know that it works (I skimmed it, and it looks legit).

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;
}
后eg是否自 2024-10-10 22:58:02

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

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

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);
}

Here is an actual solution that I wrote specifically for this problem. No recursion involved!

This returns the exact same array as you posted in your question.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文