我以这种格式获取数组数据:
例如:
array = [
"index-category",
"create-category",
"store-category",
"show-category",
"index-products",
"create-products",
"store-products",
"show-products",
"edit-products",
"index-sales",
"create-sales",
]
我需要转换为:
array {
0 => {
"index-category",
"create-category",
"store-category",
"show-category",
},
1 => {
"index-products",
"create-products",
"store-products",
"show-products",
"edit-products",
}
2 => {
"index-sales",
"create-sales",
}
}
如何转换为此数组?
PHP是自动转换的方法吗?
我认为您只是用适当的数组符号弄乱。数组符号不使用卷曲括号{
或}
。因此,如果它是一个适当的数组,则可以使用Array_chunk((
N.B :对于数组符号,我们可以使用两种不同的方法,例如$array = []
或$array=array()
<?php
$array = [
'category.index',
'category.create',
'category.store',
'category.edit',
'product.index',
'product.create',
'product.store',
'product.edit',
'city.index',
'city.create',
'city.store',
'city.edit',
];
print '<pre>';
print_r(array_chunk($array,4));
print '</pre>';
?>
输出:
Array
(
[0] => Array
(
[0] => category.index
[1] => category.create
[2] => category.store
[3] => category.edit
)
[1] => Array
(
[0] => product.index
[1] => product.create
[2] => product.store
[3] => product.edit
)
[2] => Array
(
[0] => city.index
[1] => city.create
[2] => city.store
[3] => city.edit
)
)
演示: https://eval.in/980164
编辑:
根据您的问题更新
只需在list((
的情况下尝试这样的尝试$new = array();
foreach($array as $val) {
list($item, $number) = explode('.', $val);
$new[$item][] = $val;
}
print '<pre>';
print_r(array_values($new));
print '</pre>';
演示: https://eval.in/980176
输出:
Array
(
[0] => Array
(
[0] => category.index
[1] => category.create
[2] => category.store
[3] => category.edit
)
[1] => Array
(
[0] => product.index
[1] => product.create
[2] => product.store
)
[2] => Array
(
[0] => city.index
[1] => city.create
[2] => city.store
[3] => city.edit
)
)
再次编辑
现在您的问题上有不同的初始数组,所以现在代码应该像这样。
$new = array();
foreach($array as $val) {
list($item, $number) = explode('-', $val);
$new[$number][] = $val;
}
print '<pre>';
print_r(array_values($new));
print '</pre>';
演示: https://eval.in/980251
这将是一种简单的算法来转换输入数据:
<?php
$input = [
'category.index',
'category.create',
'category.store',
'category.edit',
'product.index',
'product.create',
'product.store',
'city.index',
'city.create',
'city.store',
'city.edit',
];
$category = [];
array_walk($input, function ($entry) use (&$category) {
$tokens = explode('.', $entry);
$category[$tokens[0]][] = $entry;
});
$output = array_values($category);
print_r($output);
显然输出是:
Array
(
[0] => Array
(
[0] => category.index
[1] => category.create
[2] => category.store
[3] => category.edit
)
[1] => Array
(
[0] => product.index
[1] => product.create
[2] => product.store
)
[2] => Array
(
[0] => city.index
[1] => city.create
[2] => city.store
[3] => city.edit
)
)
这是具有琐碎修改的算法相同的算法,可以匹配您现在通过更新问题并评论此答案来突然显示的更改输入数据。
如前所述,所有这些都没有真正影响算法,微小的调整将再次提供所需的输出:
<?php
$input = [
"index-category",
"create-category",
"store-category",
"show-category",
"index-products",
"create-products",
"store-products",
"show-products",
"edit-products",
"index-sales",
"create-sales",
];
$category = [];
array_walk($input, function ($entry) use (&$category) {
$tokens = explode('-', $entry);
$category[$tokens[1]][] = $entry;
});
$output = array_values($category);
print_r($output);
这将如预期的那样导致该输出:
Array
(
[0] => Array
(
[0] => index-category
[1] => create-category
[2] => store-category
[3] => show-category
)
[1] => Array
(
[0] => index-products
[1] => create-products
[2] => store-products
[3] => show-products
[4] => edit-products
)
[2] => Array
(
[0] => index-sales
[1] => create-sales
)
)