PHP handles parent-child level selection problem

WBOY
Release: 2016-08-08 09:21:54
Original
1380 people have browsed it
/**
     * @param $data 操作的数组
     * @param string $fieldPri 唯一键名,如果是表则是表的主键
     * @param string $fieldPid 父ID键名
     * @param int $pid 一级PID的值
     * @param string $sid 子ID用于获得指定指ID的所有父ID
     * @param int $type 操作方式1=>返回多维数组,2=>返回一维数组,3=>得到指定子ID(参数$sid)的所有父
     * @param string $html 名称前缀,用于在视图中显示层次感的列表
     * @param int $level 不需要传参数(执行时调用)
     * @return array
     */
	private function channel($data, $fieldPri = 'cid', $fieldPid = 'pid', $pid = 0, $sid = null, $type = 2, $html = " ", $level = 1)
    {
        if (!$data) {
            return array();
        }
        switch ($type) {
            case 1:
                $arr = array();
                foreach ($data as $v) {
                    if ($v[$fieldPid] == $pid) {
                        $arr[$v[$fieldPri]] = $v;
                        $arr[$v[$fieldPri]]['html'] = str_repeat($html, $level - 1);
                        $arr[$v[$fieldPri]]["Data"] = self::channel($data, $fieldPri, $fieldPid, $v[$fieldPri], $sid, $type, $html, $level + 1);
                    }
                }
                return $arr;
            case 2:
                $arr = array();
                $id = 0;
                foreach ($data as $v) {
                    if ($v[$fieldPid] == $pid) {
                        $arr[$id] = $v;
                        $arr[$id]['level'] = $level;
                        $arr[$id]['html'] = str_repeat($html, $level - 1);
                        $sArr = self::channel($data, $fieldPri, $fieldPid, $v[$fieldPri], $sid, $type, $html, $level + 1);
                        $arr = array_merge($arr, $sArr);
                        $id = count($arr);
                    }
                }
                return $arr;
            case 3:
                static $arr = array();
                foreach ($data as $v) {
                    if ($v[$fieldPri] == $sid) {
                        $arr[] = $v;
                        $sArr = self::channel($data, $fieldPri, $fieldPid, $pid, $v[$fieldPid], $type, $html, $level + 1);
                        $arr = array_merge($arr, $sArr);
                    }
                }
                return $arr;
        }
    }
Copy after login

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced the issue of parent-child level selection in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template