1

I would to create a recursive function in order to retrieve data from an array and to organize then. However I have some difficulties to create the right logic. The principle must be apply to any sub level until it ends or nothing is found. I want to prevent this kind of code by repeating a foreach inside a foreach...:

$cats = get_categories($args);  
$categories = array();
foreach($cats as $cat){
    $parent = $cat->category_parent;
    if ($parent) {
            $categories['child'][$parent][$cat->cat_ID] = $cat->name;
        } else {
            $categories['parent'][$cat->cat_ID] = $cat->name;
        }
    }
}

if (isset($categories['parent']) && !empty($categories['parent'])) {
        foreach($categories['parent'] as $id => $cat){

            $new_cats[$id]['title'] = $cat;

            if (isset($categories['child'][$id])) {
                foreach($categories['child'][$id] as $child_id => $child_cat){

                    $new_cats[$child_id]['title'] = $child_cat;
                    $new_cats[$child_id]['parent_id'] = $id;

                    if (isset($categories['child'][$child_id])) {
                        foreach($categories['child'][$child_id] as $sub_child_id => $sub_child_cat){

                            $new_cats[$sub_child_id]['title'] = $sub_child_cat;
                            $new_cats[$sub_child_id]['parent_id'] = $child_id;

                        }
                    }

                }
            }
        }

    }
}
4
  • Can you please make a little example array with like 5 elements from what you have and into what form you want to get it into
    – Rizier123
    Commented Mar 16, 2016 at 16:10
  • 1
    This code makes be dizzy with all these child and subchild... And having parent and child id's on the same dimension also seems weird
    – Rizier123
    Commented Mar 16, 2016 at 16:11
  • 1
    This function is working but requieres to manually a foreach time to have a deeper depth to get child categories in my case. I think a recursive function should fit perfectly to my case but I have some difficulties to create it.
    – freaky
    Commented Mar 16, 2016 at 16:18
  • Maybe you can look into php.net/manual/en/function.array-walk-recursive.php and see if you can write a decent callback to fit your needs. This may require you to review your array structure though.
    – apokryfos
    Commented Mar 16, 2016 at 16:44

1 Answer 1

1

May be this code help you to get idea for formatting your desired array format.

<?php
// Main function
function BuildArr($arr)
{
    $formattedArr = array();
    if(!empty($arr))
    {
        foreach($arr as $val)
        {
            if($val['has_children'])
            {
                $returnArr = SubBuildArr($val['children']);  // call recursive function
                if(!empty($rs))
                {
                    $formattedArr[] = $returnArr;
                }
            }
            else
            {
                $formattedArr[] = $val;         
            }
        }
    }
    return $formattedArr;
}

// Recursive Function( Build child )
function SubBuildArr($arr)
{
    $sub_fortmattedArr = array();
    if(!empty($arr))
    {
        foreach($arr as $val)
        {
            if($val['has_children'])
            {
                $response = SubBuildArr($val['children']); // call recursive
                if(!empty($response))
                {
                    $sub_fortmattedArr[] = $response;
                }
            }
            else
            {
                $sub_fortmattedArr[] = $arr;
            }
        }
        return  $sub_fortmattedArr;
    }
}
?>

I use this code for my previous project for generating categories that upto n-th level

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.