I am using the following function to display category breadcrumbs from array
function breadcrumber($array, $id) {
// declare the storage variable without losing elements during recursion
static $result = [];
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 array_revers($result);
}
Called by:
echo implode(' -> ',breadcrumber(array_column($category,NULL,'id'),4));
Array:
$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'
),
);
this works good for single page / item page
SUV auto Parts -> Engine Related -> Engine Oil
However, when I try to display all contents from particular category in one page, this function outputs like this
SUV auto Parts -> Engine Related -> Engine Oil -> SUV auto Parts -> Engine Related -> Engine Oil
It ends up in a loop,
For example; I have 3 items under engine oil and disply it in one page in small boxes, I am expecting it to be,
item title 1
mini description
item price
SUV auto Parts -> Engine Related -> Engine Oil
item title 2
mini description
item price
SUV auto Parts -> Engine Related -> Engine Oil
item title 3
mini description
item price
SUV auto Parts -> Engine Related -> Engine Oil
Instead, the output I am getting is
item title 1
mini description
item price
SUV auto Parts -> Engine Related -> Engine Oil
item title 2
mini description
item price
SUV auto Parts -> Engine Related -> Engine Oil -> SUV auto Parts -> Engine Related -> Engine Oil
item title 3
mini description
item price
SUV auto Parts -> Engine Related -> Engine Oil -> SUV auto Parts -> Engine Related -> Engine Oil -> SUV auto Parts -> Engine Related -> Engine Oil
I am unable to solve this, I appreciate if someone will give any solution to solve this issue.
When you declare a static variable in a function it remembers its values forever. On your first call it works fine, since $result
is empty. When you call it the second time the results from the first call are still in there and the new results are just appended. That is why the breadcrumbs get longer and longer.
I rewrote your method in a way that doesn't use static variables (which easily lead to errors if you are not very careful).
function breadcrumber($array, $id)
{
if (!isset($array[$id])) return [];
$result = [];
$result[] = $array[$id]['name']; // store title text
$parent = $array[$id]['parentID']; // assign new target
unset($array[$id]); // remove possibility of an infinite loop
// prepend the recursion result to the already existing results (effectively replacing the array_reverse)
return array_merge(breadcrumber($array, $parent), $result); // recurse
}