English 中文(简体)
php 2. 阿雷拉树的田径
原标题:php Get level array from Array Tree

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.

最佳回答

具有创造力

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);
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签