1

Function getting an array(s) to loop though to get all child categories of selected category. Array look something like:

Array ( 
    [0] => Array ( 
        [id] => 7 
        [title] => Deserts 
        [slug] => deserts 
        [child] => Array ( 
            [0] => Array ( 
                [id] => 8 
                [title] => Space 
                [slug] => space 
                [child] => Array ( 
                    [0] => Array ( 
                        [id] => 
                        [title] => 
                        [slug] => 
                        [child] => Array ( ) 
                    ) 
                ) 
            ) 
        ) 
    ) 
)

I come up with a code to display all the values with foreach() loop, but I can't make things work when I need data from an exact key, for example $category['id']

public static function recursiveChildCategory($categories = [], $depth = 0) {

        // Loop through categories
        foreach($categories as $category){

        // If $category is an array.
        if(is_array($category)){

            // Drop empty arrays
            $filteredCategory = array_filter($category);

            // Loop
            self::recursiveChildCategory($filteredCategory, $depth + 1);
        } else {
            //It is not an array, so print it out
       echo str_repeat("&nbsp;&nbsp;", $depth), $category, '<br>';

        }
    }
}

currently variable $category just prints outs all values. I want to get all info separately, like $category['id'], $category['title'] and etc.

Thanks in advice

2
  • 1
    so what's the problem? use $category['id'] instead of $category Commented Jun 25, 2019 at 7:59
  • @MateiMihai I think that wont help, $category could already be the id or title etc. Commented Jun 25, 2019 at 8:14

1 Answer 1

1

Just test of the fieldname value, see

    public static function recursiveChildCategory($categories = [], $depth = 0) {
            // Loop through categories
            foreach($categories as $category){

            // If $category is an array.
            if(is_array($category)){

                // Drop empty arrays
                $filteredCategory = array_filter($category);

                // Loop
                self::recursiveChildCategory($filteredCategory, $depth + 1);
            } else {
                //It is not an array, so print it out
                if($fieldOrIndex === 'id') {
                   echo str_repeat("&nbsp;&nbsp;", $depth), $category, '<br>';
                }
            }
        }
    }

To get separated tree prints for every field in category I think you have to call the function for every field. Replacing the "id"-string with function arg and call it with "id", "title" etc.

EDIT: to have all category data for e.g. creating a link:

public static function recursiveChildCategory($categories = [], $depth = 0) {
    // Loop through categories
    foreach($categories as $category){
        echo str_repeat("&nbsp;&nbsp;", $depth);
        echo "<a href='foo_bar_{$category['id']}'>{$category['title']}</a>";
        echo '<br>';

        if(isset($category['child'])){
            // Loop
            self::recursiveChildCategory($category['child'], $depth + 1);
        } 
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your input it indeed works, but how I could wrap it into markup, to echo a link to category for example?
just add an html link echo str_repeat("&nbsp;&nbsp;", $depth), "<a href='{$URL}'>{$category}</a>", '<br>'; of course you need to create the link url.
The thing is, I need separate key values just as I described to generate link to child category, so its vital here
so you need all data of a category to generate the link?
I updated my post adding a version providing all category data. Please see if it works for you
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.