views:

54

answers:

1

Hello. I Have the next array: Array ( [1000] => Array ( [pv] => 81 )

[1101] => Array
    (
        [1102] => Array
            (
                [pv] => 33
            )

        [1103] => Array
            (
                [pv] => 15
            )

        [pv] => 72
    )

) I want to make new array from this like this:

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)
)

Generation is a level of array's deep. There are a lot of levels. Thanks.

+2  A: 

Be creative...

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);
Lekensteyn