Add multidimensional array as an array item

Asked

Viewed 398 times

-1

I am working with Windows and I am implementing a permissions-based menu, I am recovering the menu data from a table in my database. I created a model and in it I am doing the logic to retrieve the menu items and if the item has a parent_id column with the id of a parent item, then it adds it in the array as a child item (Children), my problem is, my implementation just wants to add an item, but if the parent item has more than 1 sub it must create a multidimensional array. but the exit is coming like this:

array:3 [
  0 => array:7 [
    "title" => "boletim"
    "url" => "http://myurl.com.br/aluno/boletim"
    "icon_class" => "fa-bulletin"
    "permission" => "root"
    "sort" => "1"
    "active" => "1"
    "children" => array:1 [
      0 => array:6 [
        "title" => "estatisticas"
        "url" => "http://myurl.com.br/estatisticas"
        "icon_class" => "fa-graph"
        "permission" => "admin"
        "sort" => "1"
        "active" => "1"
      ]
    ]
  ]

output using dd() function.

below is the snippet of the code I used:

private function getMenu()
    {
        return Menu::where('user_id', $this->user_id)->get();
    }

    /**
         * @return mixed
         */
        public function builder()
        {
            dd($this->formatMenu($this->getMenu()));
        }


        /**
         * @param array $menu
         *
         * @return array
         */
        public function add(array $menu) {
            return [
                'title' => $menu['display_name'],
                'url' => $menu['url'],
                'icon_class' => $menu['icon_class'],
                'permission' => $menu['permission_name'],
                'sort' => $menu['sort'],
                'active' => $menu['active']
            ];
        }

        /**
         * @param $menu
         *
         * @return array
         */
        public function formatMenu($menu)
        {
            $parentMenu = $menu->where('parent_id', null)->toArray();
            $subMenu = $menu->where('parent_id', '!=', null)->toArray();
            $result = [];

            foreach ($parentMenu as $parent) {
                $id = $parent['id'];
                $menu = $this->add($parent);

                foreach ($subMenu as $sub) {
                    $parentId = intval($sub['parent_id']);

                    if ($parentId === $id) {
                        $menu['children'] = [];
                        array_push($menu['children'], $this->add($sub));
                    }
                }

                array_push($result, $menu);
            }

            return $result;
        }

1 answer

1

i found the solution myself, basically it is to add a multidimensional array to an item of an array.

i had to add the item "Children", before I started my loop and add the child arrays to the parent array.

public function add(array $menu) {
        return [
            'title' => $menu['display_name'],
            'url' => $menu['url'],
            'icon_class' => $menu['icon_class'],
            'permission' => $menu['permission_name'],
            'sort' => $menu['sort'],
            'active' => $menu['active'],
            'children' => []
        ];
    }

public function formatMenu($menu)
    {
        $parentMenu = $menu->where('parent_id', null)->toArray();
        $subMenu = $menu->where('parent_id', '!=', null)->toArray();
        $result = [];

        foreach ($parentMenu as $parent) {
            $id = $parent['id'];
            $menu = $this->add($parent);

            foreach ($subMenu as $sub) {
                $parentId = intval($sub['parent_id']);

                if ($parentId === $id) {
                    array_push($menu['children'], $this->add($sub));
                }
            }

            array_push($result, $menu);
        }

        return collect($result)->toJson();
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.