Array:
Copy the Code code as follows:
$items = Array (
Array (' id ' = = 1, ' pid ' = + 0, ' name ' = ' 11 '),
Array (' id ' = = ' pid ' = 0, ' name ' = ' www.php.net ' 12 '),
Array (' id ' = = 2, ' pid ' = + 1, ' name ' = ' two ' Level 21 '),
Array (' id ' = +, ' pid ' = ' = ', ' name ' = ' two ' 22 '),
Array (' id ' = = 3, ' pid ' = + 1, ' name ' = ' two ' level 23 '),
Array (' id ' = ' = ', ' pid ' = ' = ', ' name ' = ' two ' 24 '),
Array (' id ' = ' + ', ' pid ' = ' + ', ' name ' = ' three ', ' Level 31 '),
Array (' id ' = = 9, ' pid ' = + 1, ' name ' = ' two ' Level 25 '),
);
Function:
Copy the Code code as follows:
function Formattree ($array, $pid = 0) {
$arr = Array ();
$tem = Array ();
foreach ($array as $v) {
if ($v [' pid '] = = $pid) {
$tem = Formattree ($array, $v [' id ']);
Determine if a subarray exists
$tem && $v [' son '] = $tem;
$arr [] = $v;
}
}
return $arr;
}