首页 > 解决方案 > 无法理解嵌套多次数组的逻辑

问题描述

我在函数中的逻辑有问题。我正在尝试将数组嵌套 X 次,以便它看起来像一棵有孩子的树。我将它与第一个孩子嵌套,但是当我深入时,我找不到删除它们的解决方案从主数组中并将它们添加到深处。“父”键是需要有子元素的父元素的 ID。任何帮助表示赞赏。

function array_search_multidim($array, $column, $key){
    return (array_search($key, array_column($array, $column)));
}

public function get_all() {
    $full_menu = $this->Site_model->get_all_menu();
    usort($full_menu, function($a, $b){
        return strcmp($a->menu_order, $b->menu_order);
    });

    foreach($full_menu as $fm) {
        $mega_menu[] = array(
        'id'        => $fm->id,
        'text'      => $fm->title,
        'href'      => $fm->link,
        'icon'      => $fm->icon,
        'target'    => $fm->target,
        'title'     => $fm->name,
        'order'     => $fm->menu_order,
        'parent'    => $fm->parent
        );

        if($fm->parent != 0) {
            $child_menu[] = array(
            'id'        => $fm->id,
            'text'      => $fm->title,
            'href'      => $fm->link,
            'icon'      => $fm->icon,
            'target'    => $fm->target,
            'title'     => $fm->name,
            'order'     => $fm->menu_order,
            'parent'    => $fm->parent
            );
        }
    }

    foreach($child_menu as $cm) {
        $mega_menu[$this->array_search_multidim($mega_menu,'id',$cm['parent'])]['children'][] = array(
        'id'        => $cm['id'],
        'text'      => $cm['text'],
        'href'      => $cm['href'],
        'icon'      => $cm['icon'],
        'target'    => $cm['target'],
        'title'     => $cm['title'],
        'order'     => $cm['order'],
        'parent'    => $cm['parent']
        );
    }

    echo '<pre>';
        print_r($mega_menu);
        echo '</pre>';
    }

现在我收到这样的数组

Array
(
    [0] => Array
        (
            [id] => 1
            [text] => Начало
            [href] => http://localhost/roni/#top
            [icon] => fas fa-home
            [target] => _self
            [title] => Начало
            [order] => 1
            [parent] => 0
        )

    [1] => Array
        (
            [id] => 4
            [text] => Споделен хостинг
            [href] => http://localhost/roni/#shared
            [icon] => fas fa-home
            [target] => _blank
            [title] => shared
            [order] => 1
            [parent] => 3
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [text] => Софтуер
                            [href] => http://localhost/roni/#software
                            [icon] => fas fa-code
                            [target] => _self
                            [title] => software
                            [order] => 2
                            [parent] => 4
                        )

                )

        )

    [2] => Array
        (
            [id] => 2
            [text] => Интернет
            [href] => http://localhost/roni/#internet2
            [icon] => fas fa-wifi
            [target] => _top
            [title] => Интернет
            [order] => 2
            [parent] => 0
        )

    [3] => Array
        (
            [id] => 5
            [text] => Софтуер
            [href] => http://localhost/roni/#software
            [icon] => fas fa-code
            [target] => _self
            [title] => software
            [order] => 2
            [parent] => 4
        )

    [4] => Array
        (
            [id] => 3
            [text] => Хостинг
            [href] => http://localhost/roni/#hosting
            [icon] => fas fa-home
            [target] => _self
            [title] => hosting
            [order] => 3
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [text] => Споделен хостинг
                            [href] => http://localhost/roni/#shared
                            [icon] => fas fa-home
                            [target] => _blank
                            [title] => shared
                            [order] => 1
                            [parent] => 3
                        )

                )

        )

    [5] => Array
        (
            [id] => 6
            [text] => Сервиз
            [href] => http://localhost/roni/#service
            [icon] => fas fa-wrench
            [target] => _self
            [title] => service
            [order] => 5
            [parent] => 0
        )

    [6] => Array
        (
            [id] => 7
            [text] => Контакти
            [href] => http://localhost/#contacts
            [icon] => fas fa-address-book
            [target] => _self
            [title] => contacts
            [order] => 6
            [parent] => 0
        )

)

标签: phpcodeigniter

解决方案


在 get_all 函数的第一个 foreach 中,您每次都将 $full_menu arrach 的元素添加到 $mega_menu,如果 $fm->parent != 0 则事件(这意味着每个子元素都保存到数组的第一级)。接下来你处理孩子。您必须仅保存(在数组的第一维)具有 $fm->parent == 0 的那些元素。您可以通过更改 get_all 中的条件来做到这一点。

function array_search_multidim($array, $column, $key){
    return (array_search($key, array_column($array, $column)));
}

public function get_all() {
    $full_menu = $this->Site_model->get_all_menu();
    usort($full_menu, function($a, $b){
        return strcmp($a->menu_order, $b->menu_order);
    });

    foreach($full_menu as $fm) {
        if($fm->parent == 0) {
            $mega_menu[] = array(
                'id' => $fm->id,
                'text' => $fm->title,
                'href' => $fm->link,
                'icon' => $fm->icon,
                'target' => $fm->target,
                'title' => $fm->name,
                'order' => $fm->menu_order,
                'parent' => $fm->parent
            );
        } else {
            $child_menu[] = array(
                'id'        => $fm->id,
                'text'      => $fm->title,
                'href'      => $fm->link,
                'icon'      => $fm->icon,
                'target'    => $fm->target,
                'title'     => $fm->name,
                'order'     => $fm->menu_order,
                'parent'    => $fm->parent
            );
        }
    }

    foreach($child_menu as $cm) {
        $mega_menu[$this->array_search_multidim($mega_menu,'id',$cm['parent'])]['children'][] = array(
            'id'        => $cm['id'],
            'text'      => $cm['text'],
            'href'      => $cm['href'],
            'icon'      => $cm['icon'],
            'target'    => $cm['target'],
            'title'     => $cm['title'],
            'order'     => $cm['order'],
            'parent'    => $cm['parent']
        );
    }

    echo '<pre>';
    print_r($mega_menu);
    echo '</pre>';
}

推荐阅读