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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?