PHP:将线性数组转换为树结构数组

时间:2018-02-23 13:47:37

标签: php sorting multidimensional-array tree

我有一个像bellow

的数组
Array
(
[0] => Array
    (
        [parent_id] => 0
        [child_id] => 1
        [uuid] => a707aa7f-2180-4cb5-9227-57c948491731
        [sdi] => 
        [serial] => 03466720000004033
        [gs1_id] => urn:epc:id:sscc:0346672.0000004033
        [type] => CONTAINER
    )

[1] => Array
    (
        [parent_id] => 1
        [child_id] => 2
        [uuid] => 5bd9da67-90eb-4fb1-b25a-0f534efd661f
        [sdi] => SDI-5bd9da67-90eb-4fb1-b25a-0f534efd661f
        [serial] => 100000003718
        [gs1_id] => urn:epc:id:sgtin:0369499.232915.100000003718
        [type] => PRODUCT
    )

[2] => Array
    (
        [parent_id] => 2
        [child_id] => 3
        [uuid] => b3224592-0268-4853-8700-03f53e759fa1
        [sdi] => SDI-b3224592-0268-4853-8700-03f53e759fa1
        [serial] => 100000042535
        [gs1_id] => urn:epc:id:sgtin:0369499.032915.100000042535
        [type] => PRODUCT
    )
)

但我需要使用parent_idchild_id从数组中获取树结构数据,数组大小可能是100,000。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

编写递归函数,以便将数组从线性转换为树。我已经为你的用例写了这样一个函数。

请查看以下代码:

/**
 * Recursively sort an array of hierarchically. Childs will be placed under a 'children' member of their parent term.
 */
function sort_array_hierarchically(Array &$linear, Array &$into, $parentId = 0) {

    foreach ($linear as $i => $elem) {
        if ($elem["parent_id"] == $parentId) {
            array_push($into,$elem);
        }
    }

    foreach ($into as $k => $topElem) {
        $into[$k]["children"] = [];
        sort_array_hierarchically($linear, $into[$k]["children"], $topElem["child_id"]);
    }

}

$linear_array = [
    ["parent_id"=>0,"child_id"=>1,"name"=>"foo"],
    ["parent_id"=>1,"child_id"=>2,"name"=>"bar"],
    ["parent_id"=>0,"child_id"=>3,"name"=>"lol"],
    ["parent_id"=>1,"child_id"=>4,"name"=>"sure"],
    ["parent_id"=>4,"child_id"=>5,"name"=>"never"],
    ["parent_id"=>3,"child_id"=>6,"name"=>"never"]
    ];

$into = [];
sort_array_hierarchically($linear_array,$into);
var_dump($into);

输出如下:

array(2) {
  [0]=>
  array(4) {
    ["parent_id"]=>
    int(0)
    ["child_id"]=>
    int(1)
    ["name"]=>
    string(3) "foo"
    ["children"]=>
    array(2) {
      [0]=>
      array(4) {
        ["parent_id"]=>
        int(1)
        ["child_id"]=>
        int(2)
        ["name"]=>
        string(3) "bar"
        ["children"]=>
        array(0) {
        }
      }
      [1]=>
      array(4) {
        ["parent_id"]=>
        int(1)
        ["child_id"]=>
        int(4)
        ["name"]=>
        string(4) "sure"
        ["children"]=>
        array(1) {
          [0]=>
          array(4) {
            ["parent_id"]=>
            int(4)
            ["child_id"]=>
            int(5)
            ["name"]=>
            string(5) "never"
            ["children"]=>
            array(0) {
            }
          }
        }
      }
    }
  }
  [1]=>
  array(4) {
    ["parent_id"]=>
    int(0)
    ["child_id"]=>
    int(3)
    ["name"]=>
    string(3) "lol"
    ["children"]=>
    array(1) {
      [0]=>
      array(4) {
        ["parent_id"]=>
        int(3)
        ["child_id"]=>
        int(6)
        ["name"]=>
        string(5) "never"
        ["children"]=>
        array(0) {
        }
      }
    }
  }
}