2 回答
TA贡献1827条经验 获得超7个赞
您可以使用循环大大简化它foreach,尤其是每次获取键和值以帮助构建数组。
这也使用第一级的键explode()将结果添加到using ,但也只是将值添加到数组的末尾 using ... $newArray$newArray[$mainKey][][]
foreach ( $array as $mainKey => $elements ) {
foreach ( $elements as $subKey => $value ){
$newData = explode("_", $subKey);
$newData[] = $value;
$newArray[$mainKey][] = $newData;
}
}
用你的测试数据给出......
Array
(
[company_info] => Array
(
[0] => Array
(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
[1] => Array
(
[0] => currency
[1] => period
[2] => 0
[3] => 20
)
)
[finance] => Array
(
[0] => Array
(
[0] => values
[1] => period
[2] => 0
[3] => 30
)
)
)
我只是注意到我丢失了第二个company_info数据,所以这意味着值将始终是数组,除非你真的只在需要时才需要它们是数组。
TA贡献1828条经验 获得超6个赞
$new_array=[];
foreach($array as $category => $tmp ){
foreach($tmp as $key => $value){
$exp = explode('_', $key);
$exp[] = $value;
$new_array[ $category ][] = $exp;
}
}
- 2 回答
- 0 关注
- 89 浏览
添加回答
举报