Saikios Saikios - 5 months ago 14
JSON Question

Obtaining the last level of a json tree on php

I have a json tree with categories,

I would like to obtain the last level of elements for each different category.

For example on this 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
}
]
}
]
}
]


The result would be an array (T-shirts, Jeans, Small Leathers, boots, cocktail)

What I was thinking is to decode it on an array and search filter the array with all the categories that are null, but I'm not sure if it's the best option because the object have different levels.

(I'm sorry for the English)

Answer

Json string is not a valid. It can be made valid by enclosing the json string in {}.

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

Decode the json string in to php opject

$json_object = json_decode($json);

Use this recursive function to fetch get all the last level categories in a array.

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));

Output:

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