如何在php中分离数组?
我使用以下格式获取阵列数据: 例如:如何在php中分离数组?,php,Php,我使用以下格式获取阵列数据: 例如: array = [ "index-category", "create-category", "store-category", "show-category", "index-products", "create-products", "store-products", "show-products", "edit-products", "index-sales", "
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是一种自动转换的方法吗?我想你只是把正确的数组表示法搞砸了。数组表示法否使用大括号
{
或}
。因此,如果它是一个合适的数组,那么您可以使用
N.B:对于数组表示法,我们可以使用两种不同的方法,例如$array=[]
或$array=array()
演示:
编辑:
根据您的问题更新
简单地试着用这个
再次编辑
由于您现在对问题有不同的初始数组,所以现在代码应该是这样的
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($val形式的数组){
列表($item,$number)=分解('-',$val);
$new[$number][]=$val;
}
打印“”;
打印(数组值($new));
打印“”;
演示:这是一个转换输入数据的简单算法:
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
)
)
这里是相同的算法,只需稍加修改,就可以通过更新问题并对该答案进行注释来匹配您现在突然呈现的更改后的输入数据 正如前面在评论中提到的,所有这些都不会真正影响算法,微小的调整将再次提供所需的输出:
不,没有这样做的功能。你需要自己写。没有自动功能。但非常简单的要求是:循环原始值,分解值,检查
分解[0]
,如果是新数组,则推到新数组的现有索引,否则推到新数组的现有索引。方括号/圆括号-不卷曲;)@嗨,请写一个例子。谢谢。显示您首先做了什么,我可以纠正您代码中的任何问题,但我不是为您编写的。我假设他们希望按字符串的第一部分进行拆分,即product
和city
,而不仅仅是每4个元素。您好,我的问题是update。以数组为例,技术上正确;最好希望数组是静态的/已经处于正确的顺序。@请查看我的最新编辑。当你改变要求时,它应该对你有用。嗨,我的问题是更新。在数组中是一个例子。@Wreigh我没有区别。您从输入中删除了一个条目。当然,这与我发布的算法无关。输出将如预期的那样。@Wreigh我更新了答案,以匹配您在问题中给出的确切输入数据(因此在第二类中删除了条目)。正如所说:这并没有改变一件事。很明显,现在输出也少了一个条目…谢谢。我运行rhis代码,但显示了以下错误:explode()希望参数2是字符串,数组给定
为什么?对我的答案投了反对票:P,我没有抄袭你的答案@arkascha,你可以清楚地看到我的答案与你的不同。顺便说一下,我只是添加/编辑我的答案,因为OP更改了问题,甚至没有查看您的解决方案
$new = array();
foreach($array as $val) {
list($item, $number) = explode('.', $val);
$new[$item][] = $val;
}
print '<pre>';
print_r(array_values($new));
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
)
[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>';
<?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
)
)