0

I have an single array of Hierarchical categories. Index of the array is the category_id like::

[8846] => Array
    (
        [category_id] => 8846
        [title] => Tsting two
        [description] => Tsting two
        [subtype] => categories
        [type] => object
        [level] => 2
        [parent_category] => 8841
        [tags] => new
        [name] => Tsting two
    )

each value has its parent_category value, I have around 500 elements in the array, what is the best way to make it.

Process i followed:

krsort categories array, so that all the child categories are at the beginning, then

function makeHierarchical() {
  foreach($this->categories as $guid => $category) {
    if($category['level'] != 1)
    $this->multilevel_categories[$category['parent_category']][$guid] = $category;
}

}

but this is not working, it does it only for first level.

Can someone point out me the error.

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
Chetan Sharma
  • 2,539
  • 5
  • 25
  • 41

1 Answers1

1

You might be able to use the answer I posted yesterday to a similar question:

Convert a series of parent-child relationships into a hierarchical tree?

Essentially it's the same, you just have more fields in your arrays. Basically you need to traverse the array recursively.

Community
  • 1
  • 1
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185