需求:需要对数组按照序号逐级显示,下级带缩进。
private function _getCategoryName($data,$kg=''){ static $num = 1; static $retrun = array(); if(!empty($data)){ foreach ($data as $key => $value){ $retrun[] = $num++.'.'.$kg.$value['name']; if(!empty($value['children'])){ $this->_getCategoryName($value['children'],$kg.'——'); } } } return $retrun; }
根据序号查找ID:
/** * 获取产品分类ID * @access private * @param array $data * @return array */ private function _getCategoryId($data, $k){ static $num = 1; static $category_id = 0; if($num==1){ $category_id = 0; } if(!empty($data)){ foreach ($data as $key => $value){ if(empty($category_id)){ if($num++ == $k){ $category_id = $value['category_id']; $num = 1; break; }elseif(!empty($value['children'])){ $this->_getCategoryId($value['children'], $k); } } } } return $category_id; }