我有以下数组(注意" Cap3"):
$myarray = array(
"Cap1" => array(
"Sub11" => 1 ,
) ,
"Cap2" => array(
"Sub21" => 2 ,
"sub22" => 3 ,
) ,
"Cap3" => array(
4
) ,
"Cap4" => array(
"Sub41" => 5 ,
) ,
) ;
对于Cap 1,2,4,array_walk_recursive工作正常。但是我遇到了Cap3的问题。我得到0 =>但我想得到以下数据:
$result = array(
"Sub11" => 1 ,
"Sub21" => 2 ,
"sub22" => 3 ,
"Cap3" => 4 ,
"Sub41" => 5 ,
) ;
知道我怎么能得到这个吗?不幸的是我无法改变输入数组。
非常感谢。
答案 0 :(得分:0)
这是你的答案: -
$result =[];
foreach($myarray as $key=>$val){
foreach($val as $k => $v){
if($k !== 0){
$result[$k] = $v;
}else{
$result[$key] = $v;
}
}
}
echo '<pre>'; print_r($result);
<强>输出强>: -
Array
(
[Sub11] => 1
[Sub21] => 2
[sub22] => 3
[Cap3] => 4
[Sub41] => 5
)