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);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。