杉哥的个人博客

php递归遍历多级分类

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* [indexAction_white 返回[树状结构]格式化后的树]
* @return [type] [description]
*/
public function areaData_white()
{
$objs = db($this->table_name)->field('id,parent_id,area_name')->select();
$arr = $this->__format_priv($objs);
return json($arr);
}
/**
* [__format_priv 与[树状结构]格式化数据, [递归]]
* @param [type] $objs [description]
* @param [type] $parent_id [description]
* @return [type] [description]
*/
private function __format_priv($objs, $parent_id = null)
{
$arr = [];
if ($objs && is_array($objs)) {
foreach ($objs as $v) {
if ($v['parent_id'] == $parent_id) {
$arr[] = [
'id' => $v['id'],
'text' => $v['area_name'],
'children' => $this->__format_priv($objs, $v['id'])
];
}
}
}
return $arr;
}
/** * [indexAction_white 返回[树状结构]格式化后的树] * @return [type] [description] */ public function areaData_white() { $objs = db($this->table_name)->field('id,parent_id,area_name')->select(); $arr = $this->__format_priv($objs); return json($arr); } /** * [__format_priv 与[树状结构]格式化数据, [递归]] * @param [type] $objs [description] * @param [type] $parent_id [description] * @return [type] [description] */ private function __format_priv($objs, $parent_id = null) { $arr = []; if ($objs && is_array($objs)) { foreach ($objs as $v) { if ($v['parent_id'] == $parent_id) { $arr[] = [ 'id' => $v['id'], 'text' => $v['area_name'], 'children' => $this->__format_priv($objs, $v['id']) ]; } } } return $arr; }
/**
     * [indexAction_white 返回[树状结构]格式化后的树]
     * @return [type] [description]
     */
    public function areaData_white()
    {
        $objs = db($this->table_name)->field('id,parent_id,area_name')->select();
        $arr = $this->__format_priv($objs);
        return json($arr);
    }

    /**
     * [__format_priv 与[树状结构]格式化数据, [递归]]
     * @param  [type] $objs      [description]
     * @param  [type] $parent_id [description]
     * @return [type]            [description]
     */
    private function __format_priv($objs, $parent_id = null)
    {
        $arr = [];
        if ($objs && is_array($objs)) {
            foreach ($objs as $v) {
                if ($v['parent_id'] == $parent_id) {
                    $arr[] = [
                        'id' => $v['id'],
                        'text' => $v['area_name'],
                        'children' => $this->__format_priv($objs, $v['id'])
                    ];
                }
            }
        }
        return $arr;
    }
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* 家谱树(面包屑导航)
* @param $id
* @return array
*/
public function getPlist($id)
{
$arr = collection($this->select())->toArray();
return $this->_familytree($arr, $id);
}
/**
* 家谱树
* @param $arr
* @param $id
* @return array
*/
private function _familytree($arr, $id)
{
$list = array();
foreach ($arr as $v) {
if ($v['id'] == $id) {
$list[] = $v['area_name'];
$list = array_merge($this->_familytree($arr, $v['parent_id']),$list );
}
}
return $list;
}
/** * 家谱树(面包屑导航) * @param $id * @return array */ public function getPlist($id) { $arr = collection($this->select())->toArray(); return $this->_familytree($arr, $id); } /** * 家谱树 * @param $arr * @param $id * @return array */ private function _familytree($arr, $id) { $list = array(); foreach ($arr as $v) { if ($v['id'] == $id) { $list[] = $v['area_name']; $list = array_merge($this->_familytree($arr, $v['parent_id']),$list ); } } return $list; }
/**
 * 家谱树(面包屑导航)
 * @param $id
 * @return array
 */
public function getPlist($id)
{
    $arr = collection($this->select())->toArray();
    return $this->_familytree($arr, $id);
}

/**
 * 家谱树
 * @param $arr
 * @param $id
 * @return array
 */
private function _familytree($arr, $id)
{
    $list = array();
    foreach ($arr as $v) {
        if ($v['id'] == $id) {
            $list[] = $v['area_name'];
            $list = array_merge($this->_familytree($arr, $v['parent_id']),$list );
        }
    }
    return $list;
}