通过指针实现二维数组转换为树形结构

 PHP基础  2022-11-23  admin  126  170

/**
 * 通过指针实现二维数组转换为树形结构
 * @param array $data 数据
 * @param string $childrenname 子数据名
 * @param string $keyName 数据key名
 * @param string $pidName 数据上级key名
 * @return array
 */
function array2Tree(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'pid'): array
{
    $list = array();
    foreach ($data as $value) {
        $list[$value[$keyName]] = $value;
    }
    static $tree = array();
    foreach ($list as $item) {
        if (isset($list[$item[$pidName]])) {
            $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
        } else {
            $tree[] = &$list[$item[$keyName]];
        }
    }
    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] => 宝安区
                                        )

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

                                )

                        )

                )

        )

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

)
PS E:\www\shop\branches\shop3> php test4.php
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] => 宝安区
                                        )

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

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

)


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

相关推荐


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{