views:

39

answers:

1

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.

+1  A: 

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

http://stackoverflow.com/questions/2915748/how-can-i-convert-a-series-of-parent-child-relationships-into-a-hierarchical-tree/2915920#2915920

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

Tatu Ulmanen
Thanks for the help, can you please tell me where was i doing mistake in my code
Chetan sharma