doupao2277 2013-09-20 10:05
浏览 47
已采纳

如何使用递归函数在PHP中获取父数组元素

I have PHP code as below which will print :

id:3deep:2path:image31.jpg

id:4deep:2path:image32.jpg

when I input deep = 2. However I would like to get parent array of this deep which is id = 2. Is there anyways which I can do this?

Note: Please see the result when I input deep as below:

> Deep = 1
  ID = 0
  ID = 1
  ID = 2
  ID = 7
  ID = 8

> Deep = 2
  ID = 2
  ID = 3
  ID = 4

> Deep = 3
  ID = 4
  ID = 5
  ID = 6

Please check my code online at : http://phpfiddle.org/main/code/xxn-7ay

PHP Code

$obj = '{
  "images": {
    "deep": "1",
    "id": "0",
    "path": "image1.jpg",
    "image": [
      {
        "deep": "1",
        "id": "1",
        "coordinate": "(x,y),(x,y)",
        "path": "image2.jpg"
      },
      {
        "deep": "1",
        "id": "2",
        "coordinate": "(x,y),(x,y)",
        "path": "image3.jpg",
        "image": [
          {
            "deep": "2",
            "id": "3",
            "coordinate": "(x,y),(x,y)",
            "path": "image31.jpg"
          },
          {
            "deep": "2",
            "id": "4",
            "coordinate": "(x,y),(x,y)",
            "path": "image32.jpg",
            "image": [
              {
                "deep": "3",
                "id": "5",
                "coordinate": "(x,y),(x,y)",
                "path": "image321.jpg"
              },
              {
                "deep": "3",
                "id": "6",
                "coordinate": "(x,y),(x,y)",
                "path": "image322.jpg"
              }
            ]
          }
        ]
      },
      {
        "deep": "1",
        "id": "7",
        "coordinate": "(x,y),(x,y)",
        "path": "image4.jpg"
      },
      {
        "deep": "1",
        "id": "8",
        "coordinate": "(x,y),(x,y)",
        "path": "image5.jpg"
      }
    ]
  }
}';


$objArray = json_decode($obj,true);


$deep = 2;
$arr = all($objArray['images'], $deep);
display($arr);

function all($a, $d){

    $temp = array();
    if(is_array($a)){

        foreach($a as $v){  
            all($v, $d);
        }
        if(isset($a['deep']) && $d == $a['deep']){
            $temp['id'] = $a['id'];
            $temp['deep'] = $a['deep'];         
            $temp['path'] = $a['path'];


                $s = 
                    'id:'.$a['id'].
                    'deep:'.$a['deep'].
                    'path:'.$a['path'];

            display($s);
        }
    }   
    return ;
}

function display($a){
    echo "<pre>";
    print_r($a);
    echo "</pre>";
}
  • 写回答

1条回答 默认 最新

  • dpiw16824 2013-09-20 10:34
    关注

    Idk exactly what you want, but I changed your code a bit to filter in a nicer way and it adds the parent key to each $image so you can reference that afterwards.

    You can set the $deep = null paramater to just add the parent to the image instead of filtering.

    I hope you don't mind I changed the code style a bit, easier to read ( I hope :D ) Here's the code within phpfiddle: http://phpfiddle.org/main/code/8q0-c43

    <?php
    
    $obj = '{
      "images": {
        "deep": "1",
        "id": "0",
        "path": "image1.jpg",
        "image": [
          {
            "deep": "1",
            "id": "1",
            "coordinate": "(x,y),(x,y)",
            "path": "image2.jpg"
          },
          {
            "deep": "1",
            "id": "2",
            "coordinate": "(x,y),(x,y)",
            "path": "image3.jpg",
            "image": [
              {
                "deep": "2",
                "id": "3",
                "coordinate": "(x,y),(x,y)",
                "path": "image31.jpg"
              },
              {
                "deep": "2",
                "id": "4",
                "coordinate": "(x,y),(x,y)",
                "path": "image32.jpg",
                "image": [
                  {
                    "deep": "3",
                    "id": "5",
                    "coordinate": "(x,y),(x,y)",
                    "path": "image321.jpg"
                  },
                  {
                    "deep": "3",
                    "id": "6",
                    "coordinate": "(x,y),(x,y)",
                    "path": "image322.jpg"
                  }
                ]
              }
            ]
          },
          {
            "deep": "1",
            "id": "7",
            "coordinate": "(x,y),(x,y)",
            "path": "image4.jpg"
          },
          {
            "deep": "1",
            "id": "8",
            "coordinate": "(x,y),(x,y)",
            "path": "image5.jpg"
          }
        ]
      }
    }';
    
    $objArray = json_decode($obj, true);
    
    function filter_deep(array $input, $deep = null, $parent = null) {
        // store $results so we can return it
        $result = array();
    
        // loop over the $input
        foreach ($input as $image) {
            // set the parent passed alogn
            $image['parent'] = $parent;
    
            // add $image to $result if 'deep' == $deep
            if ($deep === null || $image['deep'] == $deep) {
                $result[] = $image;
            }
    
            // if the $image contains more child images recursively run this function again
            //  and merge the result with what we already have
            if (isset($image['image']) && is_array($image['image'])) {
                $result = array_merge($result, filter_deep($image['image'], $deep, $image));
            }
        }
    
        return $result;
    }
    
    function display_image($image) {
        return "id:{$image['id']} deep:{$image['deep']} path:{$image['path']} parent_id:{$image['parent']['id']} <br />
    ";
    }
    
    // loop over the images that have deep == 2 and display them
    foreach (filter_deep(array($objArray['images']), 2) as $image) {
        echo display_image($image);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?