三维数怎么合并子数组
程序员文章站
2022-04-05 22:58:12
...
将上图的数组输出为下图格式的
多谢!!
回复内容:
将上图的数组输出为下图格式的
多谢!!
如果确定第三维固定只有一个键值对的话,第三重循环可以忽略
$result_arr = array();
foreach($arr as $key => $data){
foreach($data as $val) {
$second_key = key($val);
$result_arr[$key][$second_key] = $val[$second_key];
}
}
$arr = array(
'初级' =>array(
0=>array(
2=>95,
),
1=>array(
3 => 423,
)
),
'二级' => array(
0=>array(
4=>22,
),
1=>array(
5 => 43,
)
),
);
foreach ($arr as $key => $value) {
foreach ($value as $k => $v) {
foreach ($v as $ks => $vs) {
$arrs[$key][$ks] = $vs;
}
}
}
var_dump($arr);
var_dump($arrs);