将 PHP 个类别数组转换为 HTML 个多个子类别的面包屑

Convert PHP Array of categories to HTML breadcrumbs for multiple sub categories

我有一个 PHP 这样的类别数组

$category = array(
    1 => array(
        'id' => 1,
        'parentID' => 0,
        'name' => 'SUV auto parts'
    ),
        2 => array(
            'id' => 2,
            'parentID' => 1,
            'name' => 'Engine Related'
        ),
            3 => array(
                'id' => 3,
                'parentID' => 2,
                'name' => 'Spark Plugs'
                        ),
            4 => array(
                'id' => 4,
                'parentID' => 2,
                'name' => 'Engine Oil'
                        ),
        5 => array(
            'id' => 5,
            'parentID' => 1,
            'name' => 'Body related'
        ),
    6 => array(
        'id' => 6,
        'parentID' => 0,
        'name' => 'Sedan auto parts'
    ),
        7 => array(
            'id' => 7,
            'parentID' => 6,
            'name' => 'Engine Related'
            ),
        );

我已经成功地显示了这样的菜单树,

SUV auto parts
-- Engine Related
---- Spark Plugs
---- Engine Oil
-- Body Related 
Sedan auto parts
-- Engine Related

我正在尝试使用

制作面包屑
function breadcrumber($array,$id){
    static $result=[];  // declare the storage variable without losing elements during recursion
    if(isset($array[$id])){  // if target exists
        $result[]=$array[$id]['name'];  // store title text
        $parent=$array[$id]['parentID'];  // assign new target
        unset($array[$id]);  // remove possibility of an infinite loop
        breadcrumber($array,$parent);  // recurse
    }
    return $result;
}

echo implode(' -> ',breadcrumber(array_column($category,NULL,'id'),4));

小功能,很好用但是...

  1. 我不知道用 HTML /category.php?id=
  2. 来显示它
  3. 显示

Engine Oil -> Engine Related -> SUV auto Parts

而不是我希望它像

SUV auto Parts -> Engine Related -> Engine Oil

我也尝试过这种方法 似乎解决了我的两个问题..但是这不会为一个 parent 和一个 child 生成面包屑。

Sedan auto Parts -> Engine Related

仅在

时显示

Parent Category -> Child Category -> Child category

如果有人能提供任何解决方案来纠正这些问题,我将不胜感激。


预期输出将是

SUV auto Parts -> Engine Related -> Engine Oil

<a href="/category/1">SUV auto Parts</a> -> <a href="/category/2">Engine Related</a> -> <a href="/category/4">Engine Oil</a>

或仅针对一个 child 类别;

Sedan auto Parts -> Engine Related

   <a href="/category/6">Sedan auto Parts</a> -> <a href="/category/7">Engine Related</a>

#1 的解决方案

感谢@IncredibleHat

正在替换

$result[]=$array[$id]['name'];

$result[]='<a href="/category.php?id='. $array[$id]['id'] .'">'. $array[$id]['name'] .'</a>';

给出输出 Engine Oil -> Engine Related -> SUV auto Parts

<a href="/category.php?id=4">Engine Oil</a> -> <a href="/category.php?id=2">Engine Related</a> -> <a href="/category.php?id=1">SUV auto Parts</a>

#2 的解决方案

使用 PHP 数组函数调用 array_reverse https://www.php.net/manual/en/function.array-reverse.php

替换

return $result;

return array_reverse($result);

现在,输出将是; SUV auto Parts -> Engine Related -> Engine Oil

<a href="/category.php?id=1">SUV auto Parts</a> -> <a href="/category.php?id=2">Engine Related</a> -> <a href="/category.php?id=4">Engine Oil</a>