I tried to parse a JSON using PHP from url. I need a list of item that have no children - means children
field should be empty array and parent_id
shouldn't be 0
- means not be parents.
JSON:
{
"body": {
"data": [
{
"id": 1,
"name": "car",
"parent_id": "0",
"children": [
{
"id": 2,
"name": "bmw",
"parent_id": "1",
"children": [
{
"id": 4,
"name": "bmw i8",
"parent_id": "2",
"children": []
}
]
},
{
"id": 3,
"name": "mustang",
"parent_id": "1",
"children": []
}
]
},
{
"id": 5,
"name": "clothes",
"parent_id": "0",
"children": []
},
{
"id": 6,
"name": "mobile",
"parent_id": "0",
"children": [
{
"id": 7,
"name": "apple",
"parent_id": "6",
"children": [
{
"id": 9,
"name": "Iphone 12 pro",
"parent_id": "7",
"children": []
},
{
"id": 10,
"name": "Iphone 11",
"parent_id": "7",
"children": []
}
]
},
{
"id": 8,
"name": "Xiaomi",
"parent_id": "6",
"children": []
}
]
}
]
}
}
Output Expected:
[4,3,9,10,8]
This is my php code that i tried and doesn't work.
$CategoryUrl = file_get_contents(self::CATEGORY_URL,true);
$array = json_decode($CategoryUrl,true);
$list = array();
foreach( $array['body']['data'] as $value ){
if (($value['parent_id'] != 0) && empty($value['children'])) {
foreach( $value['children'] as $val ){
$list[] = $val;
}
}
}
print_r($list);
My Answer:
"categories": [
{
"id": 1,
"name": "Category 1",
"parent_id": 0,
"children": [
{
"id": 2,
"name": "Subcategory 1",
"parent_id": 1,
"children": []
},
{
"id": 3,
"name": "Subcategory 2",
"parent_id": 1,
"children": [
{
"id": 4,
"name": "Subcategory 3",
"parent_id": 3,
"children": []
}
]
}
]
},
{
"id": 5,
"name": "Category 2",
"parent_id": 0,
"children": []
},
{
"id": 6,
"name": "Category 3",
"parent_id": 0,
"children": [
{
"id": 7,
"name": "Subcategory 4",
"parent_id": 6,
"children": []
}
]
}
]
}
PHP code to find the latest children in nested categories:
php
$json = '{
"categories": [
{
"id": 1,
"name": "Category 1",
"parent_id": 0,
"children": [
{
"id": 2,
"name": "Subcategory 1",
"parent_id": 1,
"children": []
},
{
"id": 3,
"name": "Subcategory 2",
"parent_id": 1,
"children": [
{
"id": 4,
"name": "Subcategory 3",
"parent_id": 3,
"children": []
}
]
}
]
},
{
"id": 5,
"name": "Category 2",
"parent_id": 0,
"children": []
},
{
"id": 6,
"name": "Category 3",
"parent_id": 0,
"children": [
{
"id": 7,
"name": "Subcategory 4",
"parent_id": 6,
"children": []
}
]
}
]
}';
$data = json_decode($json, true);
function findLatestChildren($categories) {
$latestChildren = [];
foreach ($categories as $category) {
if (!empty($category['children'])) {
$latestChildren = array_merge($latestChildren, findLatestChildren($category['children']));
} else {
if ($category['parent_id'] != 0) {
$latestChildren[] = $category;
}
}
}
return $latestChildren;
}
$latestChildren = findLatestChildren($data['categories']);
print_r($latestChildren);
This PHP code will recursively search for the latest children in the nested categories and return a list of items that have no children and are not parents.
Rate this post
3 of 5 based on 5901 votesComments