2016-06-14 5 views
0

カテゴリがあるjsonツリーを持っていますphpのjsonツリーの最後のレベルを取得

それぞれのカテゴリごとに最後のレベルの要素を取得したいと思います。このJSONの例

[ 
    { 
     "category_id": "3", 
     "parent_id": "2", 
     "name": "Women", 
     "categories": [ 
     { 
      "category_id": "11", 
      "parent_id": "3", 
      "name": "Clothing", 
      "categories": [ 
      { 
       "category_id": "30", 
       "parent_id": "11", 
       "name": "T-shirts", 
       "categories": null 
      }, 
      { 
       "category_id": "33", 
       "parent_id": "11", 
       "name": "jeans", 
       "categories": null 
      } 
      ] 
     } 
     ] 
    }, 
    { 
     "category_id": "5", 
     "parent_id": "2", 
     "name": "Footwear ", 
     "categories": [ 
     { 
      "category_id": "15", 
      "parent_id": "5", 
      "name": "Rings", 
      "categories": [ 
      { 
       "category_id": "51", 
       "parent_id": "15", 
       "name": "Small Leathers", 
       "categories": null 
      } 
      ] 
     }, 
     { 
      "category_id": "16", 
      "parent_id": "5", 
      "name": "Bands", 
      "categories": [ 
      { 
       "category_id": "41", 
       "parent_id": "16", 
       "name": "boots", 
       "categories": null 
      } 
      ] 
     }, 
     { 
      "category_id": "48", 
      "parent_id": "5", 
      "name": "Bracelets", 
      "categories": [ 
      { 
       "category_id": "55", 
       "parent_id": "48", 
       "name": "Cocktail", 
       "categories": null 
      } 
      ] 
     } 
     ] 
    } 
    ] 

結果は、私が考えていたどのような配列(Tシャツ、ジーンズ、小レザー、ブーツ、カクテル)

だろうが、それを解読することです配列と検索は、すべてのカテゴリがnullの配列をフィルタリングしますが、オブジェクトが異なるレベルを持つため、これが最適なオプションかどうかはわかりません。

(英語は申し訳ありません)

+0

まあ、そうです。最初にデコードする必要があります。 PHPはjsonが何であるかを知らない。それはちょっとしたテキストです。 array_filter()を適切な再帰コールバックとともに使用して配列をスキャンすることはできますが、配列を通過して一致するノードを見つけるために "パス"を保持する必要がある場合は、 s)。 –

+0

今、私は別のエンドポイントで自分のツリーにマッチするためにこれを使用しているので、私はパスを保持しませんが、フィルタを実行するのがベストプラクティスか、再帰関数を使用する方が良いかどうかはわかりませんnullフィールドをasuming – Saikios

+0

これは有効なJSONではないので、答えをテストするために誰が問題を解決しようとしているのか分かりません。 – AbraCadaver

答えて

1

Json文字列は有効ではありません。 json文字列を{}で囲むことで有効にすることができます。

$json_object = json_decode($json); 

opject PHPに

$json = '{"categories": [ 
{ 
    "category_id": "3", 
    "parent_id": "2", 
    "name": "Women", 
    "categories": [ 
    { 
     "category_id": "11", 
     "parent_id": "3", 
     "name": "Clothing", 
     "categories": [ 
     { 
      "category_id": "30", 
      "parent_id": "11", 
      "name": "T-shirts", 
      "categories": null 
     }, 
     { 
      "category_id": "33", 
      "parent_id": "11", 
      "name": "jeans", 
      "categories": null 
     } 
     ] 
    } 
    ] 
}, 
{ 
    "category_id": "5", 
    "parent_id": "2", 
    "name": "Footwear ", 
    "categories": [ 
    { 
     "category_id": "15", 
     "parent_id": "5", 
     "name": "Rings", 
     "categories": [ 
     { 
      "category_id": "51", 
      "parent_id": "15", 
      "name": "Small Leathers", 
      "categories": null 
     } 
     ] 
    }, 
    { 
     "category_id": "16", 
     "parent_id": "5", 
     "name": "Bands", 
     "categories": [ 
     { 
      "category_id": "41", 
      "parent_id": "16", 
      "name": "boots", 
      "categories": null 
     } 
     ] 
    }, 
    { 
     "category_id": "48", 
     "parent_id": "5", 
     "name": "Bracelets", 
     "categories": [ 
     { 
      "category_id": "55", 
      "parent_id": "48", 
      "name": "Cocktail", 
      "categories": null 
     } 
     ] 
    } 
    ] 
} 
]}'; 

デコードJSON文字列は、アレイ内のすべての最後のレベルのカテゴリを取得取得するために、この再帰関数を使用してください。

function getLastCategories($object){ 
     $last_categories = array(); 
     if(is_array($object->categories)){ 
      foreach($object->categories as $categories){ 
       $last_categories = array_merge($last_categories, getLastCategories($categories)); 
      } 
     }else{ 
      $last_categories[]=$object->name; 
     } 
     return $last_categories; 
    } 

print_r(getLastCategories($json_object)); 

出力:

Array 
(
    [0] => T-shirts 
    [1] => jeans 
    [2] => Small Leathers 
    [3] => boots 
    [4] => Cocktail 
) 
1

あなたは再帰的なイテレータを使用してこれを行うことができます。

$categories = json_decode($json); 

// create the iterator  
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($categories)); 

// iterate recursively  
foreach ($iterator as $key => $subcategories) { 

    // if the 'categories' key is empty for the current level... 
    if ($key == 'categories' && !$subcategories) { 

     // then add the value of the 'name' key for the current level to your result array. 
     $result[] = $iterator->getSubIterator()->offsetGet('name'); 
    } 
} 

これは最も効率的な方法ではありませんが、コードは非常に簡単です。

関連する問題