php 无限级二维数组分类生成树形数据

 PHP基础  2022-11-23  admin  127  171

/**
 * 无限级二维数组分类生成树形数据
 * @param $menusList
 * @param int $pid
 * @param array $navList
 * @return array
 */
function array2Tree($arr, $pid = 0)
{
    $tree = [];
    foreach ($arr as $key => $val) {
        if ($val['pid'] == $pid) {
            unset($arr[$key]);
            $val['children'] = array2Tree($arr, $val['id']);
            if ($val['children']) {
                $val['expand'] = true;
            }
            $tree[] = $val;
        }
    }
    return $tree;
}

测试:


$data = [
    [
        'id' => '1',
        'pid' => '0',
        'name' => '广东'
    ],
    [
        'id' => '2',
        'pid' => '1',
        'name' => '深圳'
    ],
    [
        'id' => '3',
        'pid' => '2',
        'name' => '宝安区'
    ],
    [
        'id' => '4',
        'pid' => '2',
        'name' => '南山区'
    ],
    [
        'id' => '10',
        'pid' => '0',
        'name' => '浙江'
    ],

];

print_r(array2Tree($data));
Array
(
    [0] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => 广东
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [pid] => 1
                            [name] => 深圳
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [pid] => 2
                                            [name] => 宝安区
                                            [children] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 4
                                            [pid] => 2
                                            [name] => 南山区
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                            [expand] => 1
                        )

                )

            [expand] => 1
        )

    [1] => Array
        (
            [id] => 10
            [pid] => 0
            [name] => 浙江
            [children] => Array
                (
                )

        )

)


如果文章对您有帮助,点击下方的广告,支持一下作者吧!

相关推荐


Ghostscript pdf转图片的常用方法

Ghostscript是PostScript®语言和PDF文件的解释器。它可以根据GNU GPL Affero许可证获得,也可以从Artifex Software,Inc.获得商业使用许可。它已经积极开发了30多年,在此期间已经移植到多个不同的系统。Ghostscript由PostScript解释器层和图形库组成。https://www.ghostscript.com/ gswin64c.exe

阿里云域名续费优惠口令

【优惠口令】com英文域名续费:商标优选即买即用cn英文域名续费:商标智能在线检索xin域名注册:只争朝夕xin(普通词)续费:不负韶华xin(溢价词)续费:抱诚守信【如何使用口令】优惠口令限定适用后缀和行为,需验证成功后才可使用,以订单实际支付价格为准。口令不定期更新,仅限PC端,仅限普通词,限时限量。优惠口令与其他优惠(代金券、折扣、满减等)不同时使用。

php 判断远程文件是否存在

php 判断远程文件是否存在if(!function_exists('check_remote_file_exists')){ /** *判断远程文件是否存在. *@param$urlstringhttp或https开头的绝对地址 *@returnstring *@Date:2020-07-3116:47:06 *@Author:centphp.com */ fun

js 判断数组的值是否完全一致

/** *判断数组的值是否完全一致 *@paramarr *@returns{boolean} *@authorcentphp.com */ functionisAllEqual(arr){ if(arr.length>0){ return!arr.some(function(value,index){ returnvalue!==arr[0]; }); }else{