<?php
$item = array(
array('id'=>1,'pid' => 0, 'name'=>'安徽省' ),
array('id'=>2,'pid' => 0, 'name'=>'浙江省' ),
array('id'=>3,'pid' => 1, 'name'=>'合肥市' ),
array('id'=>4,'pid' => 3, 'name'=>'长丰县' ),
array('id'=>5,'pid' => 1, 'name'=>'安庆市' ),
);
function dg($array, $pid = 0){
$arr = array();
$temp = array();
foreach ($array as $v) {
if ($v['pid'] == $pid) {
$temp = dg($array, $v['id']);
//判断是否存在子数组
if($temp)
{
$v['son'] = $temp;
}
$arr[] = $v;
}
}
return $arr;
}
$array = dg($item);
echo "<pre>";
print_r($array);
php递归
最新推荐文章于 2024-04-08 05:15:00 发布