0

我有一个像这样的数组:

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

我可以splitid字段作为路径,然后我想将它们分类成一棵树......我试过这个:

$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-Array。

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' => ...
      ),
    ),
  ),
);

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

4

3 回答 3

1

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

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);
于 2013-07-11T12:49:10.570 回答
0

使 $s 引用 $res,使用&. 这将通过引用而不是按值传递数据。

$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] => Array
                (
                    [id] => foo.bar
                )

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

                )

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

                )

        )

    [data] => Array
        (
            [id] => foo
        )

)
于 2013-07-11T12:47:40.620 回答
0

递归是你的答案。

你需要一些类似的东西:

<?php

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

function createTree($path) {
    if ( count($path) === 1 ) {
        return array($path[0]=>array('data'=>'some random data here'));
    } else {
        $currentRoot = array_shift($path);
        return array($currentRoot => createTree($path));
    }
}

$result = array();

foreach ( $a as $item ) {
    $result = array_merge_recursive(
        $result,
        createTree(
            explode('.',$item['id'])
        )
    );
}

// $result now is what you wanted
于 2013-07-11T12:48:47.177 回答