0

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

id:3deep:2path:image31.jpg
id:4deep:2path:image32.jpg

当我输入 deep = 2 时。但是我想获得这个深度的父数组,即 id = 2。有什么办法可以做到这一点?

注意:当我输入深度时,请查看结果如下:

> 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:

$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>";
}
4

1 回答 1

1

Idk 正是您想要的,但是我稍微更改了您的代码以更好地进行过滤,并且它parent为每个代码添加了密钥,$image以便您以后可以引用它。

您可以将$deep = null参数设置为仅添加parentimage而不是过滤。

我希望你不介意我改变了代码样式,更容易阅读(我希望:D)这是 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 />\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);
}
?>
于 2013-09-20T10:34:06.620 回答