在 PHP 7.2 中,将一个树形结构的多维数组转换为二维数组的实现

1、HTTP 请求,获取租户列表(基于当前租户 ID,自根租户自上而下),获取到一个树形结构的多维数组,无限级循环

    /**
     * HTTP 请求,获取租户列表(基于当前租户 ID,自根租户自上而下)
     *
     * @param string $groupId 租户ID
     *
     * @return array
     * 格式如下:
     * [
     *     'group_id' => 'c10e87f39873512a16727e17f57456a5', // 租户ID
     *     'group_pid' => '', // 租户父级 ID
     *     'group_code' => '1800000001', // 租户标识
     *     'group_name' => '广东省', // 租户名称
     *     'is_open' => 1, // 租户状态,0:关闭;1:开启
     *     'group_logo' => '/uploads/cmc_group_logo/20190214/1550128076-ccLZyN.png', // 租户 LOGO 地址
     *     'open_type' => 'self-reg', // 开通方式,self-reg:自助开通;admin-open:后台开通
     *     'source' => 'cmc', // 开通途径
     *     'add_time' => '1970-01-01 08:00:01', // 添加时间
     *     'update_time' => '2019-03-11 14:43:33', // 更新时间
     *     'child' => [
     *         [
     *             'group_id' => '015ce30b116ce86058fa6ab4fea4ac63',
     *             'group_pid' => 'c10e87f39873512a16727e17f57456a5',
     *             'group_code' => '1800000005',
     *             'group_name' => '深圳市',
     *             'is_open' => 1,
     *             'group_logo' => '/uploads/cmc_group_logo/20190222/1550817342-Rr99M2.jpg',
     *             'open_type' => 'admin-open',
     *             'source' => '',
     *             'add_time' => '2018-04-26 10:05:43',
     *             'update_time' => '2019-03-11 14:43:38',
     *             'child' => [
     *                [
     *                     'group_id' => '7a4db30bbab43d2c410ea32e877492a1',
     *                     'group_pid' => '015ce30b116ce86058fa6ab4fea4ac63',
     *                     'group_code' => '88888888',
     *                     'group_name' => '罗湖区',
     *                     'is_open' => 1,
     *                     'group_logo' => '',
     *                     'open_type' => 'admin-open'
     *                     'source' => '',
     *                     'add_time' => '2018-06-22 17:40:05',
     *                     'update_time' => '2019-03-11 14:41:29',
     *                     'child' => [
     *                         ],
     *                 ],
     *                 [
     *                     'group_id' => 'chinamcloud',
     *                     'group_pid' => '015ce30b116ce86058fa6ab4fea4ac63',
     *                     'group_code' => '1800000056',
     *                     'group_name' => '南山区',
     *                     'is_open' => 1,
     *                     'group_logo' => '',
     *                     'open_type' => 'admin-open',
     *                     'source' => '',
     *                     'add_time' => '2018-07-25 16:17:26',
     *                     'update_time' => '2019-03-11 14:30:50',
     *                     'child' => [
     *                         ],
     *                 ],
     *             ],
     *         ],
     *     ],
     * ]
     *
     *
     * @throws ServerErrorHttpException
     */
    public static function httpGetAllGroupTree($groupId)
    {
        /* HTTP 请求,获取租户列表(基于当前租户 ID,自根租户自上而下) */
        $httpCmcConsoleGroup = new HttpCmcConsoleGroup();
        $httpCmcConsoleGroupGetAllGroupTree = $httpCmcConsoleGroup->getAllGroupTree($groupId);

        if ($httpCmcConsoleGroupGetAllGroupTree === false) {
            if ($httpCmcConsoleGroup->hasErrors()) {
                $firstError = '';
                foreach ($httpCmcConsoleGroup->getFirstErrors() as $message) {
                    $firstError = $message;
                    break;
                }
                throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '201002'), ['first_error' => $firstError])), 201002);
            } elseif (!$httpCmcConsoleGroup->hasErrors()) {
                throw new ServerErrorHttpException('Framework service console HTTP request failed for unknown reasons.');
            }
        }

        return $httpCmcConsoleGroupGetAllGroupTree['data'];
    }

2、获取租户列表(二维数组),将一个树形结构的多维数组转换为二维数组

    /**
     * 获取子级租户列表(二维数组)
     *
     * @param array $groupTree 租户列表(树形)
     *
     * @return array
     * 格式如下:
     * [
     *     '015ce30b116ce86058fa6ab4fea4ac63' => [
     *         'group_id' => '015ce30b116ce86058fa6ab4fea4ac63', // 租户ID
     *         'group_pid' => 'c10e87f39873512a16727e17f57456a5', // 租户父级 ID
     *         'group_code' => '1800000005', // 租户标识
     *         'group_name' => '深圳市', // 租户名称
     *         'is_open' => 1, // 租户状态,0:关闭;1:开启
     *         'group_logo' => '/uploads/cmc_group_logo/20190222/1550817342-Rr99M2.jpg', // 租户 LOGO 地址
     *         'open_type' => 'admin-open', // 开通方式,self-reg:自助开通;admin-open:后台开通
     *         'source' => '', // 开通途径
     *         'add_time' => '2018-04-26 10:05:43', // 添加时间
     *         'update_time' => '2019-03-11 14:43:38', // 更新时间
     *     ],
     *     '7a4db30bbab43d2c410ea32e877492a1' => [
     *         'group_id' => '7a4db30bbab43d2c410ea32e877492a1',
     *         'group_pid' => '015ce30b116ce86058fa6ab4fea4ac63',
     *         'group_code' => '88888888',
     *         'group_name' => '罗湖区',
     *         'is_open' => 1,
     *         'group_logo' => '',
     *         'open_type' => 'admin-open',
     *         'source' => '',
     *         'add_time' => '2018-06-22 17:40:05',
     *         'update_time' => '2019-03-11 14:41:29',
     *     ],
     *     ...
     * ]
     *
     * @throws ServerErrorHttpException
     */
    public static function getChildGroupTwo($groupTree)
    {
        if (isset($groupTree['child'])) {
            if (!empty($groupTree['child'])) {
                foreach ($groupTree['child'] as $childGroup) {
                    $groupTree[$childGroup['group_id']] = $childGroup;
                    unset($groupTree[$childGroup['group_id']]['child']);
                    $groupTree['child'] = $childGroup['child'];
                    $groupTree = static::getChildGroupTwo($groupTree);
                }
            } else {
                unset($groupTree['child']);
                $groupTree = static::getChildGroupTwo($groupTree);
            }
        }

        return $groupTree;
    }

    /**
     * 获取租户列表(二维数组)
     *
     * @param object $identity 当前用户的身份实例
     *
     * @return array
     * 格式如下:
     * [
     *     'c10e87f39873512a16727e17f57456a5' => [
     *         'group_id' => 'c10e87f39873512a16727e17f57456a5', // 租户ID
     *         'group_pid' => '', // 租户父级 ID
     *         'group_code' => '1800000001', // 租户标识
     *         'group_name' => '广东省', // 租户名称
     *         'is_open' => 1, // 租户状态,0:关闭;1:开启
     *         'group_logo' => '/uploads/cmc_group_logo/20190214/1550128076-ccLZyN.png', // 租户 LOGO 地址
     *         'open_type' => 'self-reg', // 开通方式,self-reg:自助开通;admin-open:后台开通
     *         'source' => '', // 开通途径
     *         'add_time' => '1970-01-01 08:00:01', // 添加时间
     *         'update_time' => '2019-03-11 14:43:33', // 更新时间
     *     ],
     *     '015ce30b116ce86058fa6ab4fea4ac63' => [
     *         'group_id' => '015ce30b116ce86058fa6ab4fea4ac63', // 租户ID
     *         'group_pid' => 'c10e87f39873512a16727e17f57456a5', // 租户父级 ID
     *         'group_code' => '1800000005', // 租户标识
     *         'group_name' => '深圳市', // 租户名称
     *         'is_open' => 1, // 租户状态,0:关闭;1:开启
     *         'group_logo' => '/uploads/cmc_group_logo/20190222/1550817342-Rr99M2.jpg', // 租户 LOGO 地址
     *         'open_type' => 'admin-open', // 开通方式,self-reg:自助开通;admin-open:后台开通
     *         'source' => '', // 开通途径
     *         'add_time' => '2018-04-26 10:05:43', // 添加时间
     *         'update_time' => '2019-03-11 14:43:38', // 更新时间
     *     ],
     *     '7a4db30bbab43d2c410ea32e877492a1' => [
     *         'group_id' => '7a4db30bbab43d2c410ea32e877492a1',
     *         'group_pid' => '015ce30b116ce86058fa6ab4fea4ac63',
     *         'group_code' => '88888888',
     *         'group_name' => '罗湖区',
     *         'is_open' => 1,
     *         'group_logo' => '',
     *         'open_type' => 'admin-open',
     *         'source' => '',
     *         'add_time' => '2018-06-22 17:40:05',
     *         'update_time' => '2019-03-11 14:41:29',
     *     ],
     *     ...
     * ]
     *
     *
     * @throws ServerErrorHttpException
     */
    public static function getAllGroupTwo($identity)
    {
        // HTTP 请求,获取租户列表(基于当前租户 ID,自根租户自上而下)
        $httpGetAllGroupTree = CmcConsoleGroupService::httpGetAllGroupTree($identity->group_id);

        $rootGroupTwo[$httpGetAllGroupTree['group_id']] = $httpGetAllGroupTree;
        $allGroupTwo = $rootGroupTwo;
        if (isset($httpGetAllGroupTree['child'])) {
            unset($rootGroupTwo[$httpGetAllGroupTree['group_id']]['child']);

            $allGroupTree['child'] = $httpGetAllGroupTree['child'];

            $childGroupTwo = static::getChildGroupTwo($allGroupTree);

            $allGroupTwo = array_merge($rootGroupTwo, $childGroupTwo);
        }

        return $allGroupTwo;

    }

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250456.html

(0)
上一篇 2022年4月29日
下一篇 2022年4月29日

相关推荐

发表回复

登录后才能评论