0

I'm trying to push values into an array in my map function so I can group things in my view but I'm having a problem with my permission_groups array not pushing the data in the array what am I doing wrong it always says the array is empty

    public function create()
{
    $permission_groups = [];

    $permissions = Permission::all()->map(function($item, $key) use($permission_groups) {
        $group = explode(' ', $item->name);

        if(!in_array($group[1], $permission_groups)) {
            // here it's not working it's like permission_groups is nothing when I use in function
            array_push($permission_groups, $group[1]);
        }

        return [
            'id' => $item->id,
            'name' => $item->name,
            'group' => $group[1],
        ];
    });

    dd($permission_groups);

    return view('admin.roles.create')->with(compact('permissions', 'permission_groups'));
}
ONYX
  • 5,679
  • 15
  • 83
  • 146
  • If that is what you need, then this page can be closed with: https://stackoverflow.com/a/8403958/2943403 instead of being answered. – mickmackusa Dec 19 '19 at 03:07
  • Consider bruteforcing the unique check without an ever growing `in_array()` call: https://3v4l.org/MNt6r – mickmackusa Dec 19 '19 at 03:15

0 Answers0