lartree模型中jstree的嵌套数组

时间:2019-05-06 09:19:52

标签: php laravel eloquent jstree

使用自定义功能并使用&为jstree构建嵌套数组是关键点吗?我心中的想法如下所示,但无法弄清楚该怎么做

$max_level = 10;//Change this number to increase max level
$arr = [];
function buildNestedArr(&$array, $arg2, ...){}

for ($i = 1; $i <= 10; $i++) {
    $arr = buildNestedArr($arr, ...?);
}

下面是我的工作代码,可以正常工作,但是现在我需要降低100级...需要一种解决方案来解决此问题

        $me = $this->_G['user'];

        $hasMany = [
            'sponsorSon', 'sponsorSon.sponsorSon', 'sponsorSon.sponsorSon.sponsorSon', 'sponsorSon.sponsorSon.sponsorSon.sponsorSon',
            'sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon',
            'sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon',
            'sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon',
            'sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon',
            'sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon',
            'sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon.sponsorSon',
        ];

        $starter = User::with($hasMany)->find($me->id);

        $json = array();
        $json['text'] = $starter->username . ' ( ' . count($starter->sponsorSon) . ' ) ';
        $json['state']['opened'] = true;
        $json['state']['selected'] = false;
        $json['type'] = 'nochild-' . $starter->package_id;

        if ($starter->sponsorSon && count($starter->sponsorSon)) {
            $json['type'] = 'haschild-' . $starter->package_id;

            foreach ($starter->sponsorSon as $k1 => $u1) {
                $json['children'][$k1]['text'] = $u1->username . ' ( ' . count($u1->sponsorSon) . ' )  ' . '[ ' . fundFormat($u1->total_spending) . ' ]' . '<a href="' . route('user.team.info', ['id' => $u1->id]) . '" data-toggle="modal" data-target="#remote-modal" class="btn btn-primary"><i class="fas fa-eye"></i></a>';
                $json['children'][$k1]['state']['opened'] = false;
                $json['children'][$k1]['state']['selected'] = false;
                $json['children'][$k1]['type'] = 'nochild-' . $u1->package_id;

                if ($u1->sponsorSon && count($u1->sponsorSon)) {
                    $json['children'][$k1]['type'] = 'haschild-' . $u1->package_id;

                    foreach ($u1->sponsorSon as $k2 => $u2) {
                        $json['children'][$k1]['children'][$k2]['text'] = $u2->username . ' ( ' . count($u2->sponsorSon) . ' ) ' . '[ ' . fundFormat($u2->total_spending) . ' ]';
                        $json['children'][$k1]['children'][$k2]['state']['opened'] = false;
                        $json['children'][$k1]['children'][$k2]['state']['selected'] = false;
                        $json['children'][$k1]['children'][$k2]['type'] = 'nochild-' . $u2->package_id;

                        if ($u2->sponsorSon && count($u2->sponsorSon)) {
                            $json['children'][$k1]['children'][$k2]['type'] = 'haschild-' . $u2->package_id;

                            foreach ($u2->sponsorSon as $k3 => $u3) {
                                $json['children'][$k1]['children'][$k2]['children'][$k3]['text'] = $u3->username . ' ( ' . count($u3->sponsorSon) . ' ) ' . '[ ' . fundFormat($u3->total_spending) . ' ]';
                                $json['children'][$k1]['children'][$k2]['children'][$k3]['state']['opened'] = false;
                                $json['children'][$k1]['children'][$k2]['children'][$k3]['state']['selected'] = false;
                                $json['children'][$k1]['children'][$k2]['children'][$k3]['type'] = 'nochild-' . $u3->package_id;

                                if ($u3->sponsorSon && count($u3->sponsorSon)) {
                                    $json['children'][$k1]['children'][$k2]['children'][$k3]['type'] = 'haschild-' . $u3->package_id;

                                    foreach ($u3->sponsorSon as $k4 => $u4) {
                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['text'] = $u4->username . ' ( ' . count($u4->sponsorSon) . ' ) ' . '[ ' . fundFormat($u4->total_spending) . ' ]';
                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['state']['opened'] = false;
                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['state']['selected'] = false;
                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['type'] = 'nochild-' . $u4->package_id;

                                        if ($u4->sponsorSon && count($u4->sponsorSon)) {
                                            $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['type'] = 'haschild-' . $u4->package_id;

                                            foreach ($u4->sponsorSon as $k5 => $u5) {
                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['text'] = $u5->username . ' ( ' . count($u5->sponsorSon) . ' ) ' . '[ ' . fundFormat($u5->total_spending) . ' ]';
                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['state']['opened'] = false;
                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['state']['selected'] = false;
                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['type'] = 'nochild-' . $u5->package_id;

                                                if ($u5->sponsorSon && count($u5->sponsorSon)) {
                                                    $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['type'] = 'haschild-' . $u5->package_id;

                                                    foreach ($u5->sponsorSon as $k6 => $u6) {
                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['text'] = $u6->username . ' ( ' . count($u6->sponsorSon) . ' ) ' . '[ ' . fundFormat($u6->total_spending) . ' ]';
                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['state']['opened'] = false;
                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['state']['selected'] = false;
                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['type'] = 'nochild-' . $u6->package_id;

                                                        if ($u6->sponsorSon && count($u6->sponsorSon)) {
                                                            $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['type'] = 'haschild-' . $u6->package_id;

                                                            foreach ($u6->sponsorSon as $k7 => $u7) {
                                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['text'] = $u7->username . ' ( ' . count($u7->sponsorSon) . ' ) ' . '[ ' . fundFormat($u7->total_spending) . ' ]';
                                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['state']['opened'] = false;
                                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['state']['selected'] = false;
                                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['type'] = 'nochild-' . $u7->package_id;

                                                                if ($u7->sponsorSon && count($u7->sponsorSon)) {
                                                                    $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['type'] = 'haschild-' . $u7->package_id;

                                                                    foreach ($u7->sponsorSon as $k8 => $u8) {
                                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['text'] = $u8->username . ' ( ' . count($u8->sponsorSon) . ' ) ' . '[ ' . fundFormat($u8->total_spending) . ' ]';
                                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['state']['opened'] = false;
                                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['state']['selected'] = false;
                                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['type'] = 'nochild-' . $u8->package_id;

                                                                        if ($u8->sponsorSon && count($u8->sponsorSon)) {
                                                                            $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['type'] = 'haschild-' . $u8->package_id;

                                                                            foreach ($u8->sponsorSon as $k9 => $u9) {
                                                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['text'] = $u9->username . ' ( ' . count($u9->sponsorSon) . ' ) ' . '[ ' . fundFormat($u9->total_spending) . ' ]';
                                                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['state']['opened'] = false;
                                                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['state']['selected'] = false;
                                                                                $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['type'] = 'nochild-' . $u9->package_id;

                                                                                if ($u9->sponsorSon && count($u9->sponsorSon)) {
                                                                                    $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['type'] = 'haschild-' . $u9->package_id;

                                                                                    foreach ($u9->sponsorSon as $k10 => $u10) {
                                                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['children'][$k10]['text'] = $u10->username . ' ( ' . count($u10->sponsorSon) . ' ) ' . '[ ' . fundFormat($u10->total_spending) . ' ]';
                                                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['children'][$k10]['state']['opened'] = false;
                                                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['children'][$k10]['state']['selected'] = false;
                                                                                        $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['children'][$k10]['type'] = 'nochild-' . $u10->package_id;

                                                                                        if ($u10->sponsorSon && count($u10->sponsorSon)) {
                                                                                            $json['children'][$k1]['children'][$k2]['children'][$k3]['children'][$k4]['children'][$k5]['children'][$k6]['children'][$k7]['children'][$k8]['children'][$k9]['children'][$k10]['type'] = 'haschild-' . $u10->package_id;
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

0 个答案:

没有答案