时间:2021-07-01 10:21:17 帮助过:6人阅读
$array = array( 1=>array( "name"=>"粮油" ), 2=>array( "name"=>"果蔬", "chlids"=>array( 3=>array( "name"=>"水果", "chlids"=>array( 5=>array( "name"=>"苹果", "childs"=>array( 8=>array( "name"=>"青苹果", ), 9=>array( "name"=>"红富士", ) ) ), 6=>array( "name"=>"橘子" ), 7=>array( "name"=>"大鸭梨" ) ) ), 4=>array( "name"=>"蔬菜", "chlids"=>array( 3=>array( "name"=>"白菜" ), 3=>array( "name"=>"芹菜" ) ) ), ) ) );
$r = find($array, '红富士');print_r($r);function find($ar, $name='') { $res = array(); foreach($ar as $k=>$v) { if($v['name'] == $name) $res[] = $k; elseif(isset($v['childs']) && ! $res) { $res = find($v['childs'], $name); if($res) array_unshift($res, $k); } } return $res;}Array
$r = find($array, '红富士');print_r($r);function find($ar, $name='') { $res = array(); foreach($ar as $k=>$v) { if($v['name'] == $name) $res[] = $k; elseif(isset($v['childs']) && ! $res) { $res = find($v['childs'], $name); if($res) array_unshift($res, $k); } } return $res;}Array