用于文件列表的简单数组到多维数组

时间:2013-02-11 19:54:19

标签: php arrays multidimensional-array

我对编程很陌生,而且我一直试图得到一些看起来相当简单的东西,但这让我花了太长时间而且我没有感觉到我已经接近了......

我正在尝试格式化一个如下所示的数组:

Array (
    [1] = "index.php"
    [2] = "page.php"
    [3] = "sub/subpage.php"
    [4] = "sub/subpage2.php"
    [5] = "sub/subsub/subsubpage.php"
    [6] = "sub/subsub/subsubpage2.php"
    [7] = "sub2/sub2page.php"
)

进入如下所示的数组:

Array (
    [/] => Array (
              [0] => "index.php"
              [1] => "page.php"
          )
    [/sub] => Array (
              [0] => "subpage.php"
              [1] => "subpage2.php"
              [/subsub] => Array (
                  [0] => "subsubpage.php"
                  [1] => "subsubpage2.php"
              )
          )
    [/sub2] => Array (
              [0] => "sub2page.php"
          )
)

我希望这个例子展示我正在尝试做什么...这基本上是将我的原始(简单)数组重新格式化为一个数组,我可以用它来创建HTML中的某种导航(使用嵌套的ul)

提前致谢!

编辑:

我试过这个来创建多维数组......

 $parts = explode('/', trim($page["parent"], "/"));
 while ( !empty($parts) ) {
     $pageList[array_pop($parts)] = $page["filename"];
 }

 // $page = array("filename" => "example.php", "parent" => "sub/sub/")

3 个答案:

答案 0 :(得分:1)

你的意思是这样的 - 这只是一个简单的解析器

$test = array(
    "index.php",
    "page.php",
    "sub/subpage.php",
    "sub/subpage2.php",
    "sub/subsub/subsubpage.php",
    "sub/subsub/subsubpage2.php",
    "sub2/sub2page.php"
);


function buildPathArray($array)
{
    $t = array();

    foreach ($array as $file) {
        $path = "/";
        $name = $file;

        if (preg_match('~^(.*)/([^/]+)$~', $file, $m)) {
            $path = $m[1];
            $name = $m[2];
        }

        $p = &arrayPath($t, $path);

        $p[] = $name;
    }

    return $t;
}

function &arrayPath(&$array, $path = false)
{
    if ($path == false) {
        return $array;
    }
    else
    {
        if (strpos($path, '/') === false) {
            if (!isset($array[$path])) {
                $array[$path] = array();
            }

            return $array[$path];
        }
        else
        {
            preg_match('~([^/]*)/(.*)~', $path, $m);
            if (!isset($array[$m[1]])) {
                $array[$m[1]] = array();
            }

            return arrayPath($array[$m[1]], $m[2]);
        }
    }
}

答案 1 :(得分:1)

我已经准备了一个如何实现这个目标的例子。数组$from

$from = array (
    "index.php",
    "page.php",
    "sub/subpage2.php",
    "sub/subsub/subpage2.php",
    "sub2/sub2page.php",
);

将转换为$to

$to = array();
foreach($from as $element) {
    $path = explode('/', $element);
    if(count($path) === 1) {
        array_unshift($path, '/');
    }
    $_to = &$to;
    for($i=0; $i<count($path) -1; $i++) {
        if(!array_key_exists($path[$i], $_to)) {
            $_to[$path[$i]]= array();
        }
        $_to = &$_to[$path[$i]];
    }
    $_to []= $path[count($path) -1];
}
var_dump($to);

..是什么给你以下数组:

array(3) {
  '/' =>
  array(2) {
    [0] =>
    string(9) "index.php"
    [1] =>
    string(8) "page.php"
  }
  'sub' =>
  array(2) {
    [0] =>
    string(11) "subpage.php"
    'subsub' =>
    array(1) {
      [0] =>
      string(12) "subpage2.php"
    }
  }
  'sub2' =>
  array(1) {
    [0] =>
    string(12) "sub2page.php"
  }
}

答案 2 :(得分:0)

如果你想使用这样的数组,你的键值也必须在引号中。考虑:

$array = array(
    '/' => array('index.php', 'page.php'),
    '/sub' => array('blah', 'blah', 'blah'),
    '/sub2' => array('foo', 'bar', 'foobar')
);

如何使用array关键字正确设置关键值不带括号表示法。这是不正确

$array = array(
    [key] => 'value'
);

但是,如果你想向数组添加内容......

$array = array();
$array['foo'] = 'bar'; // named key value
$array[] = 'blah'; // automatic numeric key value
$array[] = array('foo', 'bar'); // md array (array inside an array)

希望这可以帮助你解决一些问题!