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

まさにあなたが望むものだと思いますが、コードを少し変更して、より良い方法でフィルタリングし、parentそれぞれにキーを追加し$imageて、後で参照できるようにします。

フィルタリングの代わりに を$deep = nullに追加するだけのパラメータを設定できます。parentimage

コードスタイルを少し変更し、読みやすくしたことを気にしないでください (希望:D) phpfiddle 内のコードは次のとおりです

<?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 に答える