Created
March 12, 2015 13:22
-
-
Save iamsujun/33727b8b1ee965ec08a4 to your computer and use it in GitHub Desktop.
多层嵌套简单梳理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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