<?php
$cate = array(
0 => array('id' => 1, 'pid' => 0, 'name' => '江西省'),
1 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
2 => array('id' => 3, 'pid' => 1, 'name' => '上饶市'),
3 => array('id' => 4, 'pid' => 3, 'name' => '广丰县'),
4 => array('id' => 5, 'pid' => 2, 'name' => '杭州市'),
5 => array('id' => 6, 'pid' => 5, 'name' => '西湖'),
6 => array('id' => 7, 'pid' => 6, 'name' => '断桥'),
);
function getLevel ($cate,$pid,$id,$i =1) {
foreach ($cate as $v) {
if ($v['pid'] == $pid) {
if($v['id']==$id){
return $i;
}
getLevel ($cate,$v['id'],$id,$i+1);
}
}
}
$L=getLevel ($cate,2,5); //等1
$L=getLevel ($cate,2,7); //这里为什么为空?
?>
稍微修改了一下,你看看可不可以