I have access to an API that returns an array of product categories/sub-categories/sub-sub-categories, like this:
Array
(
[1] => CATEGORY 1
[2] => -Subcat 1
[9] => -Subcat 2
[12] => -Subcat 3
[13] => -Subcat 4
[46] => -Subcat 5
[47] => -Subcat 6
[3] => CATEGORY 2
[4] => -Subcat 1
[68] => -Subcat 2
[69] => -Subcat 3
[154] => -Subcat 4
[5] => CATEGORY 3
[6] => -Subcat 1
[7] => -Subcat 2
[76] => --Subsubcat 1
[156] => --Subsubcat 2
[157] => --Subsubcat 3
[10] => -Subcat 3
[11] => -Subcat 4
[20] => -Subcat 5
[54] => -Subcat 6
[55] => -Subcat 7
[56] => --Subsubcat 1
[60] => --Subsubcat 2
[65] => --Subsubcat 3
[74] => --Subsubcat 4
[61] => -Subcat 8
)
I'm not sure if it gets obvious, but a sub-cat (one depth symbol: -
) is a direct child of the last cat (no depth symbol), and a sub-sub-cat (two depth symbols: -
) is the direct child of the last sub-cat (denoted by one depth symbol -
).
So, [56] => --Subsubcat 1
is the child of [55] => -Subcat 7
(its siblings are [60] => --Subsubcat 2
, [65] => --Subsubcat 3
, etc), whereas [55] => -Subcat 7
is the child of cat [5] => CATEGORY 3
...
I know this is totally awkward, but it is what it is, and I don't have access to change it either... My problem is that I need to build from the above array a hierarchical tree of any requested id... So I'd like to build a function that will accept an $id
as a param (eg 65) and will build the whole hierarchy tree, so for 65 it should return CATEGORY 3 > Subcat 7 > Subsubcat 3
...
EDIT: I won't always pass a sub-sub-cat id to the function. The id could be of a sub-cat, or even a cat, and the function should return the correct hierarchy path: So for a cat, it should return that cat's name, and for a sub-cat, it should return Cat > Subcat
Can someone help me build this? Unfortunately I can't think of the proper algorithm to perform this complex task...
My Answer:
Array
(
[0] => Array
(
[id] => 1
[name] => Category A
[parent_id] => 0
)
[1] => Array
(
[id] => 2
[name] => Sub-category A1
[parent_id] => 1
)
[2] => Array
(
[id] => 3
[name] => Sub-category A2
[parent_id] => 1
)
[3] => Array
(
[id] => 4
[name] => Sub-sub-category A2.1
[parent_id] => 3
)
[4] => Array
(
[id] => 5
[name] => Category B
[parent_id] => 0
)
[5] => Array
(
[id] => 6
[name] => Sub-category B1
[parent_id] => 5
)
[6] => Array
(
[id] => 7
[name] => Sub-category B2
[parent_id] => 5
)
)
I need to create a parent-child relationship based on the order of appearance in the array. The parent_id indicates the parent category's id. For example, Sub-category A1 has a parent_id of 1, which means it is a sub-category of Category A.
Here is an algorithm in PHP to create the parent-child relationship based on the order of appearance in the array:
php
function createParentChildRelationship($categories) {
$parentChildRelationship = [];
foreach ($categories as $category) {
$parentId = $category['parent_id'];
$categoryId = $category['id'];
if ($parentId == 0) {
$parentChildRelationship[$categoryId] = [];
} else {
$parentChildRelationship[$parentId][] = $categoryId;
}
}
return $parentChildRelationship;
}
$categories = [
['id' => 1, 'name' => 'Category A', 'parent_id' => 0],
['id' => 2, 'name' => 'Sub-category A1', 'parent_id' => 1],
['id' => 3, 'name' => 'Sub-category A2', 'parent_id' => 1],
['id' => 4, 'name' => 'Sub-sub-category A2.1', 'parent_id' => 3],
['id' => 5, 'name' => 'Category B', 'parent_id' => 0],
['id' => 6, 'name' => 'Sub-category B1', 'parent_id' => 5],
['id' => 7, 'name' => 'Sub-category B2', 'parent_id' => 5],
];
$parentChildRelationship = createParentChildRelationship($categories);
print_r($parentChildRelationship);
This algorithm will create a parent-child relationship array where the key is the parent category's id and the value is an array of its child categories' ids.
Rate this post
4 of 5 based on 7393 votesComments