首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用递归函数在PHP中获取父数组元素

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

Stack Overflow用户
提问于 2013-09-20 10:05:03
回答 1查看 235关注 0票数 0

我有PHP代码,如下所示,它将打印:

id:3 3deep:2 2path:Image31.jpg

id:4深度:2 2path:Image32.jpg

但是,我想要得到这个深度的父数组,即id = 2。有任何方法可以这样做吗?

注:请在我输入以下内容时看到结果:

代码语言:javascript
复制
> 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

请查一下我的联机代码

PHP:

代码语言:javascript
复制
$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>";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-20 10:34:06

完全按照您想要的去做,但是我修改了您的代码,以更好的方式进行过滤,它将parent键添加到每个$image中,这样您就可以在以后引用它了。

您可以将$deep = null参数设置为只将parent添加到image,而不是过滤。

我希望您不要介意我稍微更改了代码样式,更容易阅读(我希望:D )这是phpfiddle:http://phpfiddle.org/main/code/8q0-c43中的代码

代码语言:javascript
复制
<?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 />\n";
}

// loop over the images that have deep == 2 and display them
foreach (filter_deep(array($objArray['images']), 2) as $image) {
    echo display_image($image);
}
?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18914074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档