PHP面包屑与无限的孩子


PHP Breadcrumb with infinite children

希望有人能帮忙。我有一个无限数组,这意味着我可以继续向它添加子数组

类别1>子类别1>etc>etc类别1>子类别2>etc>etc

类别2>子类别1>etc>etc类别2>子类别2>etc>etc

以我目前的数据结构,这可能吗?!谢谢

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
                                (
                                )
                        )
                )
        )
)

就像estshy说的,是的,你可以做到。

我冒昧地为你试了一下,并且能够解决你的问题。

<?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>";