<?php
$itemList = array(
array("id" => 100,"categorypath" => "level1/powders", "product" => "Mens powder"),
array("id" => 200,"categorypath" => "level1/oils/sunflower", "product" => "XYZ oil"),
array("id" => 300,"categorypath" => "level1/eatable/vegetables", "product" => "carrot"),
array("id" => 400,"categorypath" => "level1/oils/sunflower", "product" => "ABC oil"),
array("id" => 500,"categorypath" => "level1/eatable/fruits", "product" => "mango"),
array("id" => 600,"categorypath" => "level1/eatable/vegetables", "product" => "spinach"),
array("id" => 700,"categorypath" => "level2/baby items/toys", "product" => "puzzle block"),
array("id" => 800,"categorypath" => "level2/baby items/toys", "product" => "trucks and cars"),
array("id" => 900,"categorypath" => "level2/baby items/clothes", "product" => "shirts"),
array("id" => 1000,"categorypath" => "level1/powders", "product" => "Womens powder"),
array("id" => 1100,"categorypath" => "level1/oils/groundnut", "product" => "GN oil"),
);
// First, group products into their categories.
$categoryList = [];
foreach ($itemList as $item){
$categoryList[$item['categorypath']][] = [
'id' => $item['id']
, 'path' => $item['categorypath']
, 'prodname' => $item['product']
];
}
// Now, create a tree structure out of the categories
$jsonTree = ['category'=>[]];
$lookupTable = [];
foreach ($categoryList as $category=>$productList){
$componentList = explode('/', $category);
$path = '';
$parent = &$jsonTree;
do {
$component = array_shift($componentList);
$path .= $component.'/';
$entry = &$lookupTable[$path];
if (!$entry){
$entry = [
'categoryName' => $component
];
$parent['category'][] = &$entry;
}
$parent = &$entry;
} while ($componentList);
$entry['products'] = $productList;
}
echo json_encode($jsonTree, JSON_PRETTY_PRINT);
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
- {
"category": [
{
"categoryName": "level1",
"category": [
{
"categoryName": "powders",
"products": [
{
"id": 100,
"path": "level1\/powders",
"prodname": "Mens powder"
},
{
"id": 1000,
"path": "level1\/powders",
"prodname": "Womens powder"
}
]
},
{
"categoryName": "oils",
"category": [
{
"categoryName": "sunflower",
"products": [
{
"id": 200,
"path": "level1\/oils\/sunflower",
"prodname": "XYZ oil"
},
{
"id": 400,
"path": "level1\/oils\/sunflower",
"prodname": "ABC oil"
}
]
},
{
"categoryName": "groundnut",
"products": [
{
"id": 1100,
"path": "level1\/oils\/groundnut",
"prodname": "GN oil"
}
]
}
]
},
{
"categoryName": "eatable",
"category": [
{
"categoryName": "vegetables",
"products": [
{
"id": 300,
"path": "level1\/eatable\/vegetables",
"prodname": "carrot"
},
{
"id": 600,
"path": "level1\/eatable\/vegetables",
"prodname": "spinach"
}
]
},
{
"categoryName": "fruits",
"products": [
{
"id": 500,
"path": "level1\/eatable\/fruits",
"prodname": "mango"
}
]
}
]
}
]
},
{
"categoryName": "level2",
"category": [
{
"categoryName": "baby items",
"category": [
{
"categoryName": "toys",
"products": [
{
"id": 700,
"path": "level2\/baby items\/toys",
"prodname": "puzzle block"
},
{
"id": 800,
"path": "level2\/baby items\/toys",
"prodname": "trucks and cars"
}
]
},
{
"categoryName": "clothes",
"products": [
{
"id": 900,
"path": "level2\/baby items\/clothes",
"prodname": "shirts"
}
]
}
]
}
]
}
]
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
{
"category": [
{
"categoryName": "level1",
"category": [
{
"categoryName": "powders",
"products": [
{
"id": 100,
"path": "level1\/powders",
"prodname": "Mens powder"
},
{
"id": 1000,
"path": "level1\/powders",
"prodname": "Womens powder"
}
]
},
{
"categoryName": "oils",
"category": [
{
"categoryName": "sunflower",
"products": [
{
"id": 200,
"path": "level1\/oils\/sunflower",
"prodname": "XYZ oil"
},
{
"id": 400,
"path": "level1\/oils\/sunflower",
"prodname": "ABC oil"
}
]
},
{
"categoryName": "groundnut",
"products": [
{
"id": 1100,
"path": "level1\/oils\/groundnut",
"prodname": "GN oil"
}
]
}
]
},
{
"categoryName": "eatable",
"category": [
{
"categoryName": "vegetables",
"products": [
{
"id": 300,
"path": "level1\/eatable\/vegetables",
"prodname": "carrot"
},
{
"id": 600,
"path": "level1\/eatable\/vegetables",
"prodname": "spinach"
}
]
},
{
"categoryName": "fruits",
"products": [
{
"id": 500,
"path": "level1\/eatable\/fruits",
"prodname": "mango"
}
]
}
]
}
]
},
{
"categoryName": "level2",
"category": [
{
"categoryName": "baby items",
"category": [
{
"categoryName": "toys",
"products": [
{
"id": 700,
"path": "level2\/baby items\/toys",
"prodname": "puzzle block"
},
{
"id": 800,
"path": "level2\/baby items\/toys",
"prodname": "trucks and cars"
}
]
},
{
"categoryName": "clothes",
"products": [
{
"id": 900,
"path": "level2\/baby items\/clothes",
"prodname": "shirts"
}
]
}
]
}
]
}
]
}
preferences:
169.17 ms | 413 KiB | 182 Q