在PHP中将数组重组为树结构

在PHP中将数组重组为树结构,php,Php,我有一个这样的数组: $array = array( array('id' => 'foo.bar'), array('id' => 'foo'), array('id' => 'foo.baz.bar'), array('id' => 'foo.bar.bar'), ); 我可以拆分id字段,将它们作为路径,然后将它们排序到树中。。。我试过这个: $result = array(); foreach($array as $elemen

我有一个这样的数组:

$array = array(
    array('id' => 'foo.bar'),
    array('id' => 'foo'),
    array('id' => 'foo.baz.bar'),
    array('id' => 'foo.bar.bar'),
);
我可以
拆分
id
字段,将它们作为路径,然后将它们排序到树中。。。我试过这个:

$result = array();

foreach($array as $element) {
    $path = explode('.', $element['id']);

    $subtree = $result;
    while(!empty($path)) {
        $path_element = array_shift($path);

        if(empty($path_element)) {
            $subtree['data'] = $element;
        } else {
            if(!is_array($subtree[$path_element])) {
                $subtree[$path_element] = array();
            }
            $subtree = $subtree[$path_element];
        }
    }
}
但我得到的只是一堆警告和一个空的
$res
-数组

PHP Notice:  Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: baz in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: foo in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
PHP Notice:  Undefined index: bar in tree.php on line 24
PHP Stack trace:
PHP   1. {main}() tree.php:0
(第24行是
$s=$s[$pe];

有什么提示吗

编辑:我想要的输出如下

$res = array(
  'foo' => array(
    'data' => ...
    'bar' => array(
      'data' => ...
      'bar' => array(
        'data' => ...
      ),
    ),
    'baz' => array(
      'bar' => array(
        'data' => ...
      ),
    ),
  ),
);

数据
元素是数组中的原始元素。

使用
创建引用$res的$s。这将通过引用而不是通过值传递数据

$a = array(
    array('id' => 'foo.bar'),
    array('id' => 'foo'),
    array('id' => 'foo.baz.bar'),
    array('id' => 'foo.bar.bar'),
);

$res = array();

foreach($a as $e) {
    $p = explode('.', $e['id']);

    $s = &$res;
    while(!empty($p)) {
        $pe = array_shift($p);
        if(empty($p)) {
            $s['data'] = $e;
        } else {

            if(!isset($s[$pe])) {
                $s[$pe] = array();
            }
            $s = &$s[$pe];
        }
    }
}

echo '<pre>';
print_r($res);
echo '</pre>';

递归是你的答案

你需要的东西大致如下:

Array
(
    [foo] => Array
    (
        [data] => ...
        [baz] => Array
            (
                [bar] => Array
                    (
                        [data] => ...
                    )

        )

        [bar] => Array
        (
            [bar] => Array
            (
                [data] => ...
            )
        )
    )
)

下面的代码生成以下结果:

$array = array(
    array('id' => 'foo.bar'),
    array('id' => 'foo'),
    array('id' => 'foo.baz.bar'),
    array('id' => 'foo.bar.bar'),
);


$res = array();

foreach($array as $e) {

    $parts = explode('.', $e['id']);

    $temp = &$res;

    foreach($parts as $key => $el) {
        if (!isset($temp[$el])) $temp[$el] = array();

        if ($key == count($parts)-1) $temp[$el] = array('data' =>  '...');
        $temp = &$temp[$el];
    }
}

print_r($res);
我重命名了你们中的一些变量


你有一个例子来说明这棵树的样子吗?你没有把任何东西放在$res中,难怪它是空的。此外,我甚至不知道它应该做什么。@Benz添加了一个例子。@LambdaDusk我首先要让你的代码可读。我是说,所有这些变量意味着什么?”$p、 $pe、$s、$res、$a、$e)。。它们其实意义不大。那个“数据”键应该从哪里来?
Array
(
    [foo] => Array
    (
        [data] => ...
        [baz] => Array
            (
                [bar] => Array
                    (
                        [data] => ...
                    )

        )

        [bar] => Array
        (
            [bar] => Array
            (
                [data] => ...
            )
        )
    )
)
$array = array(
    array('id' => 'foo.bar'),
    array('id' => 'foo'),
    array('id' => 'foo.baz.bar'),
    array('id' => 'foo.bar.bar'),
);


$res = array();

foreach($array as $e) {

    $parts = explode('.', $e['id']);

    $temp = &$res;

    foreach($parts as $key => $el) {
        if (!isset($temp[$el])) $temp[$el] = array();

        if ($key == count($parts)-1) $temp[$el] = array('data' =>  '...');
        $temp = &$temp[$el];
    }
}

print_r($res);