1

I'm getting from a service one array as the following:

0: 
category_id: 21
parent_id: 17
name: Parent
url_key: parent
is_active: 1
position: 4
level: 3
children: 
    0: 
        category_id: 25
        parent_id: 21
        name: child1
        url_key: child1
        is_active: 1
        position: 1
        level: 4
        children: 
            0: 
                category_id: 52
                parent_id: 25
                name: child1child1
                url_key: child1child1
                is_active: 1
                position: 1
                level: 5
                children: 

    1: 
        category_id: 26
        parent_id: 21
        name: child2
        url_key: child2
        is_active: 1
        position: 2
        level: 4
        children: 
            0: 
                category_id: 54
                parent_id: 26
                name: child2child1
                url_key: child2child1
                is_active: 1
                position: 1
                level: 5
                children: 


            1: 
                category_id: 55
                parent_id: 26
                name: child2child2
                url_key: child2child2
                is_active: 1
                position: 2
                level: 5
                children: 

I need to go through this array which I don't know how deep could it get and generate the url paths with them using the url_key variable like:

parent
parent/child1
parent/child1/child1
parent/child2/child1

I was trying to do it with array_walk_recursive but I can figure it out.

Any help would be appreciated.

Thanks

1
  • And what have you tried. SO is not a free code supplier Commented Aug 19, 2015 at 18:03

1 Answer 1

1

array_walk_recursive is not really clever enough for this. You have to make your own. I did this quick, I'm sure it can be improved considerably:

<?php

$categories = array(
    0 => array(
        'url_key' => 'parent',
        'children' => array(
            0 => array(
                'url_key' => 'child1',
                'children' => array(
                    0 => array(
                        'url_key' => 'child1child1',
                        'children' => array()
                    )
                )
            ),
            1 => array(
                'url_key' => 'child2',
                'children' => array(
                    0 => array(
                        'url_key' => 'child2child1',
                        'children' => array()
                    ),
                    1 => array(
                        'url_key' => 'child2child2',
                        'children' => array(
                            0 => array(
                                'url_key' => 'child2child2child1',
                                'children' => array()
                            ),
                        )
                    ),

                )
            )
        )
    )
);

$urls = array();

foreach ($categories as $category) {
    $urls = $urls + buildUrls($category, $urls);
}

function buildUrls($category, &$urls, $url_parts = array()) {
    $url_parts[] = $category['url_key'];
    $urls[] = join('/', $url_parts);
    if (!empty($category['children'])) {
        foreach ($category['children'] as $child_category) {
            $urls = $urls + buildUrls($child_category, $urls, $url_parts);
        }
    }
    return $urls;
}
var_dump($urls);

Output:

array (size=7)
  0 => string 'parent' (length=6)
  1 => string 'parent/child1' (length=13)
  2 => string 'parent/child1/child1child1' (length=26)
  3 => string 'parent/child2' (length=13)
  4 => string 'parent/child2/child2child1' (length=26)
  5 => string 'parent/child2/child2child2' (length=26)
  6 => string 'parent/child2/child2child2/child2child2child1' (length=45)
Sign up to request clarification or add additional context in comments.

1 Comment

I was doing it far more complex with array_walk_recursive -.- saving parents and levels in variables. Thanks

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.