Skip to content

Instantly share code, notes, and snippets.

@iamsujun
Created March 12, 2015 13:22
Show Gist options
  • Save iamsujun/33727b8b1ee965ec08a4 to your computer and use it in GitHub Desktop.
Save iamsujun/33727b8b1ee965ec08a4 to your computer and use it in GitHub Desktop.
多层嵌套简单梳理
<?php
/**
* 多层嵌套简单梳理
*/
$child_to_parent = array(
1 => 0,
2 => 0,
3 => 1,
4 => 1,
5 => 3,
6 => 5,
7 => 2,
8 => 0,
9 => 4,
);
$childs = array();
foreach ($child_to_parent as $c => $p) {
$childs[$p][]=$c;
}
function getChild($data, $pid)
{
$arr = array();
foreach ($data[$pid] as $key => $value) {
$arr[$value] = isset($data[$value]) ? getChild($data, $value) : 0;
}
return $arr;
}
$re = getChild($childs, 0);
print_r($childs);
print_r($re);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment