首页 > 编程语言 >php 二维数组重组成父与子关系

php 二维数组重组成父与子关系

时间:2023-04-27 09:25:39浏览次数:37  
标签:sibling fnBuilder tree grouped 二维 父与子 childrenKey php id

function buildTree(array $flat, $pidKey = 'pid', $idKey = 'id', $childrenKey = 'children') {
        $grouped = [];
        foreach ($flat as $sub) {
            $grouped[$sub[$pidKey]][] = $sub;
        }
        $fnBuilder = function(&$siblings) use ($grouped, $idKey, $childrenKey, &$fnBuilder) {
            foreach ($siblings as &$sibling) {
                $id = $sibling[$idKey];
                if (isset($grouped[$id])) {
                    $sibling[$childrenKey] = $grouped[$id];
                    $fnBuilder($sibling[$childrenKey]);
                }
            }
        };
        $tree = [];
        if (isset($grouped[0])) {
            $tree = $grouped[0];
            $fnBuilder($tree);
        }
        return $tree;
    }

 

标签:sibling,fnBuilder,tree,grouped,二维,父与子,childrenKey,php,id
From: https://www.cnblogs.com/louqianzhu/p/17357982.html

相关文章

  • 2022-04-26:给定一个数组componets,长度为A, componets[i] = j,代表i类型的任务需要耗时j
    2022-04-26:给定一个数组componets,长度为A,componets[i]=j,代表i类型的任务需要耗时j给定一个二维数组orders,长度为M,orders[i][0]代表i号订单下单时间orders[i][1]代表i号订单是哪种类型的任务,毫无疑问orders[i][1]<A一开始所有流水线都在0时刻待命,给定一个正数nums,表示流水......
  • 每日打卡一维数组和二维数组传参的几种方式
    //一组数组传参//#include<stdio.h> //voidInputArray(intn[],inta);//voidOutputArray(intn[],inta);//intmain()//{// intm[3];// InputArray(m,3);// OutputArray(m,3);// return0;//}//voidInputArray(intm[],intn)//{// for(inti=0;i<n;i++)......
  • php升级 编译安装php7 支持openeuler欧拉
    php版本下载包查询:https://www.php.net/releases/ yum-yinstallcmakelibxml2libxml2-developensslopenssl-develcurl-devellibjpeg-devellibpng-develfreetype-devellibziplibzip-devellibsodiumsqlitesqlite-develonigurumaoniguruma-devellibwebp-devel......
  • 机器学习(七):梯度下降解决分类问题——perceptron感知机算法与SVM支持向量机算法进行二
    实验2感知机算法与支持向量机算法一、预备知识1.感知机算法二、实验目的掌握感知机算法的原理及设计;掌握利用感知机算法解决分类问题。三、实验内容设计感知机算法求解,设计SVM算法求解(可调用函数库),请找出支持向量和决策超平面。四、操作方法和实验步骤1.......
  • vue使用vue-qr生成二维码
    vue-qr基础使用:第一步,先安装vue-qr插件npminstallvue-qr--save第二步,在想要生成vueQr的Vue页面引入组件importvueQrfrom'vue-qr'第三步,在components中引入VueQr组件components:{VueQr}如下:<script>importVueQrfrom'vue-qr';exportdefault{componen......
  • 记php调用chatgpt接口
    $openai_api_key='your_openai_api_key_here';$engine='davinci';//ChatGPT中的AI模型名称$prompt='Hello,canyouhelpme?';//ChatGPT中的提示信息//构造请求数据$data=array('model'=>$engine,'prompt&#......
  • php 版本号对比,1.0.5和1.15.0
    $version1='1.0.5';$version2='1.15.0';//自定义比较函数functioncompare_versions($v1,$v2){$ver1=explode('.',$v1);$ver2=explode('.',$v2);//将每个版本号中的字符串转换为整数$ver1=array_map('intval&......
  • D365: 生成Warehouse mobile app连接环境二维码
     //JSON文件格式{"ConnectionList":[{"ActiveDirectoryClientAppId":"206a394e-0dd6-44d5-a50b-796cef0d4bXX","ConnectionName":"Vyung-Test","ActiveDirectoryResource":"https://handvyung-test.......
  • pandas.DataFrame—构建二维、尺寸可变的表格数据结构
    语法格式pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)常用的几个参数解释:data:一系列数据,包括多种类型;index:索引值,行标签,默认值为RangeIndex(0,1,2,…,n);columns:列标签,默认值为RangeIndex(0,1,2,…,n);dtype:设置数据......
  • 编译部署LNMP-php8.1.18版本
    由于mysql编译会非常耗费资源,故咱们这里不做介绍,只介绍nginx的编译、php的编译以及nginx和php的联动,至于mysql如何接入,需要看具体业务,在LNMP中mysql是相对独立的,不需要特别的配置编译安装nginx新建www用户groupaddwwwuseradd-s/sbin/nologin-gwwwwww安装必要依赖yum......