doumu1212 2012-06-03 19:07
浏览 33

递归数组

I already spent some hours figuring this one out but without any usable result. Maybe someone could help me out with this one, i want to use it inside a class so I want to build a method with as input the array and as output the rebuild array

I have this array:

     Array
     (
         [0] => Array
             (
                 [5] => 0-5
                 [8] => 0-8
                 [9] => 0-9
                 [10] => 0-10
             )

         [10] => Array
             (
                 [11] => 10-11
                 [12] => 10-12
             )

         [11] => Array
             (
                 [13] => 11-13
             )

         [12] => Array
             (
                 [14] => 12-14
             )

     )

And this array should be transformed to this one:

     Array
     (
         [0] => 0-5
         [1] => 0-8
         [2] => 0-9
         [3] => Array
             (
                 [0] => 0-10
                 [1] => Array
                     (
                         [0] => 10-11
                         [1] => Array
                             (
                                 [0] => 11-13
                             )

                         [2] => 10-12
                         [3] => Array
                             (
                                 [0] => 12-14
                             )

                     )

             )

     )

EDIT1: The values of the examples are also arrays en with this I want to process some actions. For example 0-9 is one action and stops but 0-10 has children. If 0-10 is finished and return true or false the next child is fired. If true 10-11 else 10-12. It is my first time on this forum sorry for any inconvenience

EDIT2: Maybe this example helps, this is the posted string in JSON. I have a couple of rules in the database wich will help to pick the right article for the job.

    $postdata = '{"next":false, 
                  "data":{"name":"patrick",
                          "etc":"etc",
                         "MotionDetection":"yes",
                         "nameOfTheRoom": ["Room1","Room2","Room3"],  
                         "havePets":"yes"}';

 Array
 (
     [0] => 0-5
     [1] => 0-8
     [2] => 0-9
     [3] => Array
         (
             [0] => "yes" // MotionDetection
             [1] => Array
                 (
                     [0] => "yes" // Have Pets
                     [1] => Array
                         (
                             [0] => "CountRecords" // Count nameOfTheRoom
                             [1] => "insertArticle A"
                         )

                     [2] => "No" // Have Pets
                     [3] => Array
                         (
                             [0] => "CountRecords" // Count nameOfTheRoom
                             [1] => "insertArticle B"
                         )

                 )

         )

 )
  • 写回答

1条回答 默认 最新

  • douhan5547 2012-06-03 19:47
    关注

    Try this:

    function transform_arr(&$newarr, &$arr)
    {
      if(count($newarr) == 0)
      {
        if(!isset($arr[0]))
        {
           return;
        }
    
        $newarr = $arr[0];
      }
    
      $newarr2 = array();
      $k = 0;
      foreach( $newarr as $i => $value )
      {
         if( isset($arr[$i]) )
         {
           $newarr2[$k++] = $value;
           $newarr2[$k++] = $arr[$i];
    
           if(is_array($newarr2[$k - 1])) transform_arr($newarr2[$k - 1], $arr);
         }
         else
         {
           $newarr2[$k++] = $value;
         }
      }
      $newarr = $newarr2;
    }
    
    $arr = array( 
      0 => array( 5=> '0-5', 8 => '0-8', 9 => '0-9', 10 => '0-10'),
      10 => array( 11 => '10-11', 12 => '10-12'),
      11 => array( 13 => '11-13'),
      12 => array( 14 => '12-14') );
    
    $newarr = array();
    transform_arr($newarr, $arr);
    
    var_dump($arr);
    echo '<br/>';
    var_dump($newarr);
    

    Result:

      [0]=>
      array(4) {
        [5]=>
        string(3) "0-5"
        [8]=>
        string(3) "0-8"
        [9]=>
        string(3) "0-9"
        [10]=>
        string(4) "0-10"
      }
      [10]=>
      array(2) {
        [11]=>
        string(5) "10-11"
        [12]=>
        string(5) "10-12"
      }
      [11]=>
      array(1) {
        [13]=>
        string(5) "11-13"
      }
      [12]=>
      array(1) {
        [14]=>
        string(5) "12-14"
      }
    }
    
    array(5) {
      [0]=>
      string(3) "0-5"
      [1]=>
      string(3) "0-8"
      [2]=>
      string(3) "0-9"
      [3]=>
      string(4) "0-10"
      [4]=>
      array(4) {
        [0]=>
        string(5) "10-11"
        [1]=>
        array(1) {
          [0]=>
          string(5) "11-13"
        }
        [2]=>
        string(5) "10-12"
        [3]=>
        array(1) {
          [0]=>
          string(5) "12-14"
        }
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向