codiaf
codiaf

Reputation: 629

Go through an unknown deep PHP array and get values to form an url

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

Upvotes: 1

Views: 73

Answers (1)

rjdown
rjdown

Reputation: 9227

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)

Upvotes: 1

Related Questions