Home  >  Article  >  php教程  >  获取树状分类结构

获取树状分类结构

PHP中文网
PHP中文网Original
2016-05-23 08:39:201115browse

php代码

    /**
     * @param $data//数据集合
     * @param $pid
     *
     * @return array|string
     */
    public function getTree($data, $pid)
    {
        $tree = array();
        foreach ($data as $k => $v) {
            if ($v['parent_id'] == $pid) {
                $child = $this->getTree($data, $v['id']);
                if ($child) {
                    $v['childs'] = $child;
                }
                $tree[] = $v;
            }
        }

        return $tree;
    }

Statement:
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