簡體   English   中英

在PHP中將數組重組為樹結構

[英]Regroup array to tree structure in PHP

我有一個像這樣的數組:

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

我可以split id字段split為路徑,然后將它們排序為樹...

$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元素是數組中的原始元素。

下面的代碼生成以下結果:

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);

使用&使$ 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
        )

)

遞歸是您的答案。

並且您需要以下方面的東西:

<?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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM