douxing8939 2013-07-11 12:17
浏览 56
已采纳

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

I have an array like this one:

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

I can split the id fields to get them as paths, and then I'd like to sort them into a tree... I tried this:

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

But all I get is a load of warnings and an empty $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

(Line 24 is $s = $s[$pe];)

Any hint?

EDIT: My desired output would be like this

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

The data elements are the original elements from the array.

  • 写回答

3条回答 默认 最新

  • duanhuang1967 2013-07-11 12:49
    关注

    The code below generates the following result:

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

    I renamed some of you're variables...

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染