1

I have a PHP array of categories like this

$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'
            ),
        );

I have managed to display the menu tree like this,

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

I was trying to make breadcrumbs using https://stackoverflow.com/a/48598077/12677030

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

Small function, works great but...

  1. I can not figure out to display it with HTML /category.php?id=
  2. It displays

Engine Oil -> Engine Related -> SUV auto Parts

instead of which I want it to be like,

SUV auto Parts -> Engine Related -> Engine Oil

I have also tried this approach https://stackoverflow.com/a/52571259/12677030 Seems to solve both of my issues ..however this does not generate breadcrumbs for one parent one child.

Sedan auto Parts -> Engine Related

Shows only when

Parent Category -> Child Category -> Child category

I appreciate if someone will give any solution to rectify these issues.


expected output will be

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>

Or for only one child category;

Sedan auto Parts -> Engine Related

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

holland987
  • 15
  • 6

1 Answers1

0

Solution for #1

Credits to @IncredibleHat

replacing

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

with

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

gives the output 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>

Solution for #2

Using PHP array funcion called array_reverse https://www.php.net/manual/en/function.array-reverse.php

replace

return $result;

with

return array_reverse($result);

Now, Output will be; 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>
holland987
  • 15
  • 6