Search code examples
phparraysrecursionmultidimensional-arraybreadcrumbs

PHP Breadcrumb with infinite children


hope someone can help. I have this infinite array meaning I can keep adding children to it. I want to display as a breadcrumb as follows:

Category 1 > Sub Cat 1 > etc > etc Category 1 > Sub Cat 2 > etc > etc

Category 2 > Sub Cat 1 > etc > etc Category 2 > Sub Cat 2 > etc > etc

Is that possible with my current data structure?! Thanks!

Array
(
    [Category 1] => Array
        (
            [title] => Category 1
            [id] => 1
            [parentID] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [title] => Child of Category 1
                            [id] => 3
                            [parentID] => 1
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [title] => Child of Category 1
                            [id] => 4
                            [parentID] => 1
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [Category 2] => Array
        (
            [title] => Category 2
            [id] => 2
            [parentID] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [title] => Child of Category 2
                            [id] => 5
                            [parentID] => 2
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [title] => Child of Category 2
                            [id] => 6
                            [parentID] => 2
                            [children] => Array
                                (
                                )

                        )

                )

        )

)

Solution

  • Like estshy said, yeah you can do it.

    I've taken the liberty of trying it out for you and was able to accomplish your problem.

    <?php
    
    $data = array(
        "Category 1" => array(
                "title" => "Category 1",
                "id" => 1,
                "parentID" => 0,
                "children" => array(
                    "0" => array(
                        "title" => "Child of Category 1",
                        "id" => 3,
                        "parentID" => 1,
                        "children" => array()
                    ),
                    "1" => array(
                        "title" => "Child of Category 1",
                        "id" => 4,
                        "parentID" => 1,
                        "children" => array()
                    )
                )
            ),
        "Category 2" => array(
            "title" => "Category 2",
            "id" => 2,
            "parentID" => 0,
            "children" => array(
                "0" => array(
                    "title" => "Child of Category 2",
                    "id" => 5,
                    "parentID" => 2,
                    "children" => array()
                ),
                "1" => array(
                    "title" => "Child of Category 2",
                    "id" => 6,
                    "parentID" => 2,
                    "children" => array()
                )
            )
        )
    );
    
    
    function recurse($data) {
        $return = array();
        $i = 0;
    
        foreach ($data as $item) {
            $i++; // Add to the counter so we can get the proper children associated to the parent
    
            $temp = array("title" => "", "id" => 0, "parentID" => 0, "children" => array());
    
            // Define the title of the link
            if (isset($item['title']) && $item['title'] != "") $temp['title'] = $item['title'];
    
            // Define the ID of the link
            if (isset($item['id']) && $item['id'] != "") $temp['id'] = $item['id'];
    
            // Define the parent ID of the link
            if (isset($item['parentID']) && $item['parentID'] != "") $temp['parentID'] = $item['parentID'];
    
            // Define the link
            $return[$i]['link'] = $temp['title']." - (ID: ".$temp['id'].") - (Parent ID: ".$temp['parentID'].")";
    
            // Define the children of the link
            if (isset($item['children']) && is_array($item['children']) && !empty($item['children'])) {
                $return[$i]['children'] = recurse($item['children']);
                continue;
            }
        }
    
        return $return;
    }
    
    function flatten($array = array()) {
        // Return the given parameter as an array if it isn't one
        if (!is_array($array)) return array($array);
    
        $result = array(); // Initialize the result array
    
        // Loop through all of the results, merging them and making them single-dimensional
        foreach ($array as $value) $result = array_merge($result, flatten($value));
    
        return $result; // Return!
    }
    
    function define_breadcrumbs($data = array()) {
        $links = array();
    
        // Loop through all of the data items (single-dimensional) and define the breadcrumbs
        //  with that information
        foreach (recurse($data) as $item) $links[] = implode(" > ", flatten($item));
    
        return $links; // Return!
    }
    
    echo "<pre>";
    print_r(define_breadcrumbs($data));
    echo "</pre>";