我有下一个数组: 排列 ( [1000] =>排列 ( [pv] => 81 )
[1101] => Array
(
[1102] => Array
(
[pv] => 33
)
[1103] => Array
(
[pv] => 15
)
[pv] => 72
)
) 我想像这样制作新的数组:
Array(
[1000] => Array(['generation'] => 1, ['pv'] => 81)
[1101] => Array(['generation'] => 1, ['pv'] => 72)
[1102] => Array(['generation'] => 2, ['pv'] => 33)
[1103] => Array(['generation'] => 2, ['pv'] => 15)
)
生成是数组的深层次。有很多层次。 感谢。
答案 0 :(得分:3)
有创意......
function coffee($elm, $cur_key=NULL, $level=0, &$push_arr=NULL){
if(!is_array($push_arr)) $pusH_arr = array();
$level++;
foreach($elm as $key=>$val){
if($key == 'pv'){
$push_arr[$cur_key] = array(
'generation' => $level,
'pv' => $val
);
}
else if(is_array($val)){
coffee($val, $key, $level, $push_arr);
}
else{
// unexpected value
}
}
return $push_arr;
}
// Usage:
$normalised_array = coffee($array_from_your_question);