【问题标题】:PHP Breadcrumb with infinite children具有无限子级的 PHP 面包屑
【发布时间】:2014-10-03 18:44:28
【问题描述】:

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

类别 1 > 子类别 1 > 等 > 等 类别 1 > 子类别 2 > 等 > 等

类别 2 > 子类别 1 > 等 > 等 类别 2 > 子类别 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
                                (
                                )

                        )

                )

        )

)

【问题讨论】:

  • 是的。你需要某种递归函数。

标签: php arrays recursion multidimensional-array breadcrumbs


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多