PHP构建树阵列/平面阵列的面包屑

时间:2016-12-05 20:25:45

标签: php arrays

我正在构建一个产品类别数组中的数组。此数组是包含父项和子项的树数组。不,我选择其中一个孩子,我想建立这个孩子的固定链接。

我几乎就在那里,但在某个地方,我犯了一个错误,我无法弄清楚。

所以我想说我有这个类别数组。

$cat = [
    0 => [
        "id" => "32",
        "shop_id" => "19",
        "parent_id" => "0",
        "name" => "T-shirts",
        "permalink" => "t-shirts",
        "image" => "category/image.jpg",
        "description" => "",
        "order" => "0",
        "children" => [
            0 => [
                "id" => "33",
                "shop_id" => "19",
                "parent_id" => "32",
                "name" => "Dames",
                "permalink" => "t-shirts/dames",
                "image" => "category/image.jpg",
                "description" => "",
                "order" => "0",
                "children" => [
                    0 => [
                        "id" => "38",
                        "shop_id" => "19",
                        "parent_id" => "33",
                        "name" => "V-hals",
                        "permalink" => "t-shirts/dames/v-hals",
                        "image" => "category/image.jpg",
                        "description" => "",
                        "order" => "0",
                        "children" => [
                            0 => [
                                "id" => "40",
                                "shop_id" => "19",
                                "parent_id" => "38",
                                "name" => "Rood",
                                "permalink" => "t-shirts/dames/v-hals/rood",
                                "image" => "",
                                "description" => "",
                                "order" => "0",
                                "children" => [
                                    0 => [
                                        "id" => "47",
                                        "shop_id" => "19",
                                        "parent_id" => "40",
                                        "name" => "Lange mouw",
                                        "permalink" => "t-shirts/dames/v-hals/rood/lange-mouw",
                                        "image" => "",
                                        "description" => "",
                                        "order" => "0",
                                        "children" => null,
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
];

现在我在category_id 38,我想构建面包屑。为此,我需要每个类别的namepermalink

为了实现这一目标,我已将这个数组平放,因此它们都处于同一级别。那时我使用以下功能:

function build_breadcrumb_from_category($categories, $pid)
{
    $return = [];
    foreach ($categories as $category) {
        if ($pid == $category['id']) {

            $return[] = [$category['name'], $category['permalink']];

            if ($category['parent_id'] > 0) {
                # Page has parents
                $return[] = build_breadcrumb_from_category($categories, $category['parent_id']);

            }

        }
    }
    return array_reverse($return);
}

但这给了我一种"树"再次阵列。

$breadcrumb = build_breadcrumb_from_category($flat_categories, 38);
$breadcrumb = [
    0 => [
        0 => [
            0 => [
                0 => [
                    0 => [
                        0 => "T-shirts",
                        1 => "t-shirts",
                    ],
                ],
                1 => [
                    0 => "Dames",
                    1 => "t-shirts/dames",
                ],
            ],
            1 => [
                0 => "V-hals",
                1 => "t-shirts/dames/v-hals",
            ]
        ],
        1 => [
            0 => "Rood",
            1 => "t-shirts/dames/v-hals/rood",
        ],
    ],
    1 => [
        0 => "Lange mouw",
        1 => "t-shirts/dames/v-hals/rood/lange-mouw",
    ],
];

我不明白如何让这个阵列变平。我怎样才能得到一个很好的数组,只有一层,我可以做foreach

所需的输出

   $breadcrumbs = [
       [       
            0 => "T-shirts",
            1 => "t-shirts",
       ],
       [       
            0 => "Dames",
            1 => "t-shirts/dames",
       ],
       [       
            0 => "V-hals",
            1 => "t-shirts/dames/v-hals",
       ],
    ]

2 个答案:

答案 0 :(得分:2)

您可以通过索引展平数组来优化这一点 - 现在搜索循环太多(如果您想要更好的解决方案,请发布展平函数)。你拥有的功能不需要做大的改动。您应该首先构建父结构,推送当前位置,然后将其返回到更高的范围(作为其父结构):

function build_breadcrumb_from_category($categories, $pid)
{
    $result = [];
    foreach ($categories as $category) {
        if ($pid == $category['id']) {
            if ($category['parent_id'] > 0) {
                # Page has parents
                $result = build_breadcrumb_from_category($categories, $category['parent_id']);
            }

            $result[] = [$category['name'], $category['permalink']];
        }
    }
    return $result;
}

这是带索引类别列表的解决方案 - 第一个函数展平树,第二个函数使用其id索引构建breadcrub路径:

function category_index($category_tree)
{
    $result = [];
    foreach($category_tree as $category) {
        if (!empty($category['children'])) {
            $result = $result + category_index($category['children']);
        }
        unset($category['children']);
        $result[$category['id']] = $category;
    }
    return $result;
}

function category_breadcrumb($category_index, $pid)
{
    if (empty($category_index[$pid])) { return []; }

    $category = $category_index[$pid];
    $result   = ($category['parent_id']) ? category_breadcrumb($category_index, $category['parent_id']) : [];
    $result[] = [$category['name'], $category['permalink']];

    return $result;
}

答案 1 :(得分:0)

我的解决方案

function build_breadcrumb_from_category($categories, $pid)
{
    $result = [];
    foreach ($categories as $category)
    {
        if ($pid == $category['id'])
        {
            return [[$category['name'], $category['permalink']]];
        }

        $result = build_breadcrumb_from_category($category['children'], $pid);

        if (!empty($result))
        {   
            $result[] = [$category['name'], $category['permalink']];
            return $result;
        }       
    }    
}