日期:2014-05-16 浏览次数:20431 次
1.示例1
<?php
function find_child($ar, $id='id', $pid='pid') {
foreach($ar as $v) $t[$v[$id]] = $v;
foreach ($t as $k => $item){
if( $item[$pid] ) {
$t[$item[$pid]]['child'][] =&$t[$k];
unset($t[$k]);
}
}
return $t;
}
$data = array(
array('ID'=>1,'PARENT'=>0,'NAME'=>'kobe'),
array('ID'=>2,'PARENT'=>0,'NAME'=>'jama'),
array('ID'=>3,'PARENT'=>1,'NAME'=>'kobe1'),
array('ID'=>4,'PARENT'=>2,'NAME'=>'jama1'),
array('ID'=>5,'PARENT'=>0,'NAME'=>'lizhi'),
array('ID'=>6,'PARENT'=>1,'NAME'=>'kobe2'),
);
$c = find_child($data, 'ID', 'PARENT');
echo '<pre>';
print_r($c);
?>
2.示例2
<?php
function find_child($ar, $id='id', $pid='pid') {
foreach($ar as $v) $t[$v[$id]] = $v;
foreach ($t as $k => $item){
if( $item[$pid] ) {
$t[$item[$pid]]['child'][] =&$t[$k];
unset($t[$k]);
}
}
return $t;
}
$data = array(
array('ID'=>1,'PARENT'=>0,'NAME'=>'kobe'),
array('ID'=>2,'PARENT'=>0,'NAME'=>'jama'),
array('ID'=>3,'PARENT'=>1,'NAME'=>'kobe1'),
array('ID'=>4,'PARENT'=>2,'NAME'=>'jama1'),
array('ID'=>5,'PARENT'=>0,'NAME'=>'lizhi'),
array('ID'=>6,'PARENT'=>1,'NAME'=>'kobe2'),
);
$c = find_child($data, 'ID', 'PARENT');
echo '<pre>';
print_r($c);
?>