$items = array(
1 => array("id" => 1, "pid" => 0, "name" => ""),
2 => array("id" => 2, "pid" => 0, "name" => ""),
3 => array("id" => 3, "pid" => 1, "name" => ""),
4 => array("id" => 4, "pid" => 2, "name" => ""),
6 => array("id" => 6, "pid" => 3, "name" => ""),
5 => array("id" => 5, "pid" => 2, "name" => ""),
7 => array("id" => 7, "pid" => 3, "name" => "")
);
print_r($this->genTree5($items));
//
public function genTree5($items) {
foreach ($items as $item)
$items[$item["pid"]]["son"][$item["id"]] = &$items[$item["id"]];
return isset($items[0]["son"]) ? $items[0]["son"] : array();
}
the infinite classification conversion n-dimensional array I found on the Internet is completed in 5 lines. I can"t understand it. I can"t see where he returns, but he"s so awesome. Trace and debug, $items [$item ["pid"]]" son"]; just repeat it here. Then you can output a multi-dimensional array, which feels so powerful. Who can help me to analyze, this is how he executes the logic.
I"m not good at it, and I"m a little stupid. I just like code.
Array
(
[1] => Array
(
[id] => 1
[pid] => 0
[name] =>
[son] => Array
(
[3] => Array
(
[id] => 3
[pid] => 1
[name] =>
[son] => Array
(
[6] => Array
(
[id] => 6
[pid] => 3
[name] =>
)
[7] => Array
(
[id] => 7
[pid] => 3
[name] =>
)
)
)
)
)
[2] => Array
(
[id] => 2
[pid] => 0
[name] =>
[son] => Array
(
[4] => Array
(
[id] => 4
[pid] => 2
[name] =>
)
[5] => Array
(
[id] => 5
[pid] => 2
[name] =>
)
)
)
)