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>

5
  • I think his question 1. about HTML, is he wants to wrap the breadcrumb name, in an <a> anchor tag with the url and category id inserted. Something thats pretty darn difficult with just using implode. Commented Jan 9, 2020 at 19:32
  • Although changing $result[]=$array[$id]['name']; to something like $result[]='<a href="/category.php?id='. $array[$id]['id'] .'">'. $array[$id]['name'] .'</a>'; might work as you want? Commented Jan 9, 2020 at 19:36
  • @ggorlen I am expecting the output to be like, <a href="/category/1">SUV auto Parts</a> -> <a href="/category/2">Engine Related</a> -> <a href="/category/4">Engine Oil</a> or when I visit items under "Engine Related" will be like <a href="/category/6">Sedan auto Parts</a> -> <a href="/category/7">Engine Related</a> Commented Jan 9, 2020 at 19:43
  • @IncredibleHat it worked perfect, 1st issue solve. I appreciate it. Commented Jan 9, 2020 at 19:55
  • What do you mean by "I can not figure out to display"? What have you tried so far? Commented Jan 9, 2020 at 21:36

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.