0

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.

4
  • By mistake you've posted the same question twice. Please delete this question. Commented Sep 24, 2010 at 7:07
  • possible duplicate of php Get level array from Array Tree Commented Sep 24, 2010 at 7:07
  • 1
    you asked 4! very similar questions yesterday. haven't you learned anything from that solutions? this is just a tiny modification of the other ones. i don't want to be mean, but have you even tried to do this yourself - and, if you have, could you please post your code so we can teach you what's wrong with it? Commented Sep 24, 2010 at 7:09
  • It is different problems. I have some problems with arrays and recursion. Please help. Duplicate quastion has been deleted. Commented Sep 24, 2010 at 7:12

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.