在 php 上获取 json 树的最后一层

Obtaining the last level of a json tree on 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 恤、牛仔裤、小皮衣、靴子、鸡尾酒)

我的想法是在一个数组上对其进行解码,然后使用所有为空的类别搜索过滤该数组,但我不确定这是否是最佳选择,因为对象具有不同的级别。

(对不起英文)

Json 字符串无效。可以通过将 json 字符串括在 {}.

中使其有效
$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 字符串解码为 php 对象

$json_object = json_decode($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
)

您可以使用递归迭代器来做到这一点。

$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');
    }
}

这可能不是最有效的方法,但它使代码非常简单。