我正在使用权限系统,我必须将单级数组(由下划线分隔)转换为多维数组。有关解决此问题的函数的任何帮助吗?
输入数组:
Array
(
[0] => dashboard
[1] => dashboard_read
[2] => dashboard_update
[3] => dashboard_create
[4] => dashboard_delete
[5] => dashboard_search
[6] => timeplan_event_read
[7] => timeplan_event_search
[8] => timeplan_read
[9] => timeplan_search
[10] => webhotel
[11] => webhotel_read
[12] => webhotel_update
[13] => webhotel_create
[14] => webhotel_delete
[15] => webhotel_search
)
输出数组:
array(
'dashboard' => array(
'read',
'update',
'create',
'delete',
'search'
),
'timeplan' =>array(
'read',
'search',
'event' => array(
'read',
'search'
)
),
'webhotel' =>array(
'read',
'update',
'create',
'delete',
'search'
),
)
答案 0 :(得分:5)
您可以遍历数组并在' _' -character上爆炸。在此之后,您可以使用所需的树格式简单地构建新数组。
<?php
$tmp = array("dashboard","dashboard_read","dashboard_update","dashboard_create","dashboard_delete","dashboard_search","timeplan_event_read","timeplan_event_search","timeplan_read","timeplan_search","webhotel","webhotel_read","webhotel_update","webhotel_create","webhotel_delete","webhotel_search");
$newarray = array();
foreach ($tmp as $value) {
$parts = explode("_", $value);
if (count($parts) < 2)
continue;
$arr = &$newarray;
for ($i=0;$i<count($parts)-1;$i++) {
if (!array_key_exists($parts[$i], $arr)) {
$arr[$parts[$i]] = array();
}
$arr = &$arr[$parts[$i]];
}
$arr[] = $parts[count($parts)-1];
}
print_r($newarray);
---输出---
$ php -f tmp.php
Array
(
[dashboard] => Array
(
[0] => read
[1] => update
[2] => create
[3] => delete
[4] => search
)
[timeplan] => Array
(
[event] => Array
(
[0] => read
[1] => search
)
[0] => read
[1] => search
)
[webhotel] => Array
(
[0] => read
[1] => update
[2] => create
[3] => delete
[4] => search
)
)
答案 1 :(得分:0)
我之前遇到过同样的问题,但我不知道代码的确如此。我的想法是编写一个递归函数,explode('_', $string)
当前的权限,并遍历各部分。它会像
if count $parts === 1:
$result[] = $parts
else:
$result[$parts[0]] = recursive call ($parts, $result)
我希望这会带给你正确的方式......
答案 2 :(得分:0)
没有深度限制。
$arr = [];
while($path = array_pop($x))
{
$parts = explode('_',$path);
$value = array_pop($parts);
$cell = &$arr;
while($part = array_shift($parts)){
if (!isset($cell[$part])) $cell[$part] = [];
$cell = &$cell[$part];
}
if (!array_key_exists($value, $cell))
array_push($cell, $value);
}
输出:
array:3 [
"webhotel" => array:5 [
0 => "search"
1 => "delete"
2 => "create"
3 => "update"
4 => "read"
]
"timeplan" => array:3 [
0 => "search"
1 => "read"
"event" => array:2 [
0 => "search"
1 => "read"
]
]
"dashboard" => array:5 [
0 => "search"
1 => "delete"
2 => "create"
3 => "update"
4 => "read"
]
]
答案 3 :(得分:0)
[akshay@localhost tmp]$ cat test.php
<?php
$array = array("dashboard","dashboard_read","dashboard_update","dashboard_create","dashboard_delete","dashboard_search","timeplan_event_read","timeplan_event_search","timeplan_read","timeplan_search","webhotel","webhotel_read","webhotel_update","webhotel_create","webhotel_delete","webhotel_search");
function build_arr($array, $delim='_')
{
$output = array();
foreach($array as $key)
{
$main = explode($delim, $key);
if (count($main) < 2)
continue;
$bottom = &$output;
while(count($main) > 1)
{
$sub = array_shift($main);
if (!isset($bottom[$sub]))
{
$bottom[$sub] = array();
}
$bottom = &$bottom[$sub];
}
$bottom[] = $main[count($main)-1];
}
return $output;
}
// Input
print_r( $array);
// Output
print_r( build_arr($array) );
?>
<强>输出
[akshay@localhost tmp]$ php test.php
Array
(
[0] => dashboard
[1] => dashboard_read
[2] => dashboard_update
[3] => dashboard_create
[4] => dashboard_delete
[5] => dashboard_search
[6] => timeplan_event_read
[7] => timeplan_event_search
[8] => timeplan_read
[9] => timeplan_search
[10] => webhotel
[11] => webhotel_read
[12] => webhotel_update
[13] => webhotel_create
[14] => webhotel_delete
[15] => webhotel_search
)
Array
(
[dashboard] => Array
(
[0] => read
[1] => update
[2] => create
[3] => delete
[4] => search
)
[timeplan] => Array
(
[event] => Array
(
[0] => read
[1] => search
)
[0] => read
[1] => search
)
[webhotel] => Array
(
[0] => read
[1] => update
[2] => create
[3] => delete
[4] => search
)
)