How to get unlimited child categories in structured format using recursion in PHP?
author
,
A guide

How to get all child categories using recursion in PHP?

  • Total 1 Answer
  • 46
Can You answer this question?
author
,
A guide

We can get all child categories in PHP using recursion. Follow below example for better understanding:

public function get_child($product_categories)
{
    $cat = array();
    if(count($product_categories) > 0)
    {
        foreach($product_categories as $val)
        {
            $cat[$val['category_id']]['id'] = $val['category_id'];
            $cat[$val['category_id']]['name'] = $val['category'];
            if(count($val['subcategories']) > 0)
            {
                $cat[$val['category_id']]['child'] = $this->get_child($val['subcategories']);  
            }
        }          
    }
    return $cat;
}