laravel 将许多 collection 转换为单个数组
laravel convert many collection to single array
我的 foreach 有问题。我有 collection 这样的数据:
["post access", "post create", "post read", "post update", "post delete", "publish access", ...]
但我需要将其转换为:
[
"name" => "post",
"access" => false,
"create" => false,
"read" => false,
"update" => false,
"delete" => false
], [
"name" => "publish",
"access" => false
], [...]
顺便说一句,我仍在学习 laravel-permission,我为 blade 中的复选框做了这个。
我是怎么做到的?请帮我。谢谢
你必须同时使用这两个 String Helpers and Collection Methods。
$collection = collect(["post access", "post create", "post read", "post update", "post delete", "publish access", ...]);
$array = array();
$collection->each(function($item. $key){
if(Str::contains(['post','get'], $item) && (! isset($array[0]['name']))){
$array[0]['name'] = Str::before(' ', $item);
}
if (Str::contains(['access','create','read','update','delete'], $item){
$name_removed = Str::remove($array[0]['name'] . ' ', $item);
$array[0][$name_removed] = false;
}
if (Str::contains('publish access', $item){
$array[1]['name'] = Str::remove(' access', $item);
$array[1]['access'] = false;
}
}
好的,我完成了
use Illuminate\Support\Str;
$array = ["post access", "post create", "post read", "post update", "post delete", "publish access"];
foreach($array as $permission){
$generatePermissions[] = [
"name" => Str::remove(["access", "create", "read", "update","delete"], $permission)
];
if(Str::contains($permission, "access")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["access"] = false;
}elseif(Str::contains($permission, "create")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["create"] = false;
}elseif(Str::contains($permission, "read")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["read"] = false;
}elseif(Str::contains($permission, "update")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["update"] = false;
}elseif(Str::contains($permission, "delete")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["delete"] = false;
}
}
$convertedArray = array_filter($generatePermissions, function($array){
if(count($array) > 1){
return $array;
}
});
dd($convertedArray);
这是我解决的方案。谢谢
我的 foreach 有问题。我有 collection 这样的数据:
["post access", "post create", "post read", "post update", "post delete", "publish access", ...]
但我需要将其转换为:
[
"name" => "post",
"access" => false,
"create" => false,
"read" => false,
"update" => false,
"delete" => false
], [
"name" => "publish",
"access" => false
], [...]
顺便说一句,我仍在学习 laravel-permission,我为 blade 中的复选框做了这个。
我是怎么做到的?请帮我。谢谢
你必须同时使用这两个 String Helpers and Collection Methods。
$collection = collect(["post access", "post create", "post read", "post update", "post delete", "publish access", ...]);
$array = array();
$collection->each(function($item. $key){
if(Str::contains(['post','get'], $item) && (! isset($array[0]['name']))){
$array[0]['name'] = Str::before(' ', $item);
}
if (Str::contains(['access','create','read','update','delete'], $item){
$name_removed = Str::remove($array[0]['name'] . ' ', $item);
$array[0][$name_removed] = false;
}
if (Str::contains('publish access', $item){
$array[1]['name'] = Str::remove(' access', $item);
$array[1]['access'] = false;
}
}
好的,我完成了
use Illuminate\Support\Str;
$array = ["post access", "post create", "post read", "post update", "post delete", "publish access"];
foreach($array as $permission){
$generatePermissions[] = [
"name" => Str::remove(["access", "create", "read", "update","delete"], $permission)
];
if(Str::contains($permission, "access")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["access"] = false;
}elseif(Str::contains($permission, "create")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["create"] = false;
}elseif(Str::contains($permission, "read")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["read"] = false;
}elseif(Str::contains($permission, "update")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["update"] = false;
}elseif(Str::contains($permission, "delete")){
$permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["delete"] = false;
}
}
$convertedArray = array_filter($generatePermissions, function($array){
if(count($array) > 1){
return $array;
}
});
dd($convertedArray);
这是我解决的方案。谢谢