1

我对编程很陌生,我一直在尝试做一些事情,这似乎很简单,完成了,但这花了我太长时间,而且我不觉得我已经接近了......

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

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/")
4

3 回答 3

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]);
        }
    }
}
于 2013-02-11T20:11:42.347 回答
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"
  }
}
于 2013-02-11T20:12:13.763 回答
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)

希望这可以帮助你一些!

于 2013-02-11T20:01:34.933 回答