Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
ThinkPHP框架中如何将无限级分类的一维数组转换为多维数组?_创想鸟

ThinkPHP框架中如何将无限级分类的一维数组转换为多维数组?

thinkphp框架中如何将无限级分类的一维数组转换为多维数组?

无限级分类子分类读取

问题:

如何使用 thinkphp 框架读取无限级分类中的所有子分类,并以多维数组形式返回?

解决方案:

立即学习“PHP免费学习笔记(深入)”;

AVCLabs AVCLabs

AI移除视频背景,100%自动和免费

AVCLabs 268 查看详情 AVCLabs

首先,要解决这个问题,我们需要创建一个函数 getchildarea 来读取子分类,该函数将以给定的地区 id 作为参数,并以递归方式遍历所有子分类。

function getchildarea($id){    if (!$id) {        return;    }    static $area;    $area = $area ?? new appcommonmodelarea;    $result = collection($area->where(['pid' => $id])->order('id desc')->select())->toarray();    static $res = [];    if ($result) {        foreach ($result as $key => $val) {            $res[] = $val;            getchildarea($val['id']);        }    }    return $res;}

然而,这个函数返回的是一维数组。要获得多维数组,我们需要将一维数组转换为多维数组。我们可以使用 deal_list_to_tree2 函数来实现此目的:

function deal_list_to_tree2($data, $pkname = 'id', $pidname = 'parent_id', $childname = 'children_list', $is_empty_childrens = false, $rootid = ''){    $new_data = [];    if (!empty($data)) {        foreach ($data as $sordata) {            if (array_key_exists($childname, $sordata) && !empty($sordata[$childname])) {                $res = deal_list_to_tree2($data, $pkname, $pidname, $childname, $is_empty_childrens, $sordata[$pkname]);            } else {                if ($sordata[$pidname] == $rootid) {                    if ($sordata[$pkname] != $rootid) {                        $res = deal_list_to_tree2($data, $pkname, $pidname, $childname, $is_empty_childrens, $sordata[$pkname]);                    }                    if (!empty($res) && !$is_empty_childrens) {                        if (array_key_exists($childname, $sordata)) {                            if (array_key_exists($childname, $sordata)) {                                for ($i = 0; $i < count($res); $i++) {                                    $sordata[$childname][] = $res[$i];                                }                            } else {                                $sordata[$childname][] = $res;                            }                        } else {                            $sordata[$childname] = $res;                        }                    }                    $new_data[] = $sordata;                }            }        }    }    return $new_data;}

调用 deal_list_to_tree2 函数可以将一维数组转换为多维数组:

$multidimensionalData = deal_list_to_tree2($result);

现在,你将拥有一个包含所有子分类的多维数组。请注意,deal_list_to_tree2 函数是一个通用的函数,它可以用于转换任何一维数组到多维数组。

以上就是ThinkPHP框架中如何将无限级分类的一维数组转换为多维数组?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/738563.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Win10重装系统后电脑的东西还在吗?重装系统原来的文件去向
上一篇 2025年11月25日 13:56:40
《DOTA2》TI14八强出炉 夺冠热门雪碧惨遭淘汰
下一篇 2025年11月25日 13:56:40

相关推荐

发表回复

登录后才能评论
关注微信