Mount array with users Permissions

Asked

Viewed 76 times

1

I need to mount an array in PHP (LARAVEL) and I’m not finding a way to do.

I have a user system with roles and Permissions. When I view the user, I list all the Permissions, but one below the other. Ex:

>     users-index
>     users-show
>     users-create
>     roles-create
>     roles-delete

But I’d like to do the following:

Take all these Permissions and separate them by category to list in separate table, where this is the heading is the word before the "-" (USERS-index), ex array:

Arrayall = [ users = [ users-index users-show, users-create ]] roles = [ roles-create, roles-delete ] ]

I’m counting on ideas! Hugs

  • 2

    It seems that this will lead us to a giant response.

1 answer

2


If I understand correctly I think this helps you

$teuArrayDePermissoes = array('users-index','users-show','users-create','roles-create','roles-delete');
$newArray = array();
foreach($teuArrayDePermissoes as $permissao){
    $parts = explode("-", $permissao);
    $newArray[$parts[0]][] = $permissao;
}
var_dump($newArray);

Should display in the output the array you want

  • Ball show, that’s right. I just didn’t understand the part of: $newArray[$Parts[0] [ ] = .... I know that in $newArray... It will have the key as $Parts and value as permission... But why of empty conchetes after key?

  • $Parts[0] will have the first key, for example 'user' or 'roles', so this line that you did not understand will fill the array as an array or array of arrays, then you will access $newArray['users'] and it will return an array with [ users-index, users-show, users-create ]... in php you can say $array[] = ... as a shortcut to $array[x] = ... where x would be the next Indice, so you insert without a key and at the end of the array

Browser other questions tagged

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