PHP面包屑与无限的孩子

时间:2014-10-03 18:44:28

标签: php arrays recursion multidimensional-array breadcrumbs

希望有人可以提供帮助。我有这个无限的数组意味着我可以继续添加孩子。我想显示如下的面包屑:

第1类> Sub Cat 1>等等>等等 1类> Sub Cat 2>等等>等

第2类> Sub Cat 1>等等>等等 第2类> Sub Cat 2>等等>等

我目前的数据结构可以实现吗?!谢谢!

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

                        )

                )

        )

)

1 个答案:

答案 0 :(得分:0)

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