This is my array:
$array = array (
'1' => array(
'title' => 'Level1',
'nodes' => array(
'11' => array('title' => 'sub1_company1'),
'12' => array('title' => 'sub2_company1'),
'13' => array(
'title' => 'sub3_company1',
'nodes' => array(
'131' => array('title' => 'item1_sub3_company1'),
'132' => array('title' => 'item2_sub3_company1'),
),
),
),
),
'2' => array(
'title' => 'Level2',
'nodes' => array(
'21' => array('title' => 'sub1_company2'),
'22' => array('title' => 'sub2_company2'),
),
),
'3' => array(
'title' => 'Level3',
'nodes' => array(
'31' => array('title' => 'sub1_company3'),
'32' => array(
'title' => 'sub2_company3',
'nodes' => array(
'321' => array('title' => 'item1_sub2_company3'),
'322' => array(
'title' => 'item2_sub2_company3',
'nodes' => array(
'3221' => array('title' => 'item1_sub3_company3'),
),
),
),
),
),
),
'4' => array('title' => 'Level4'),);
What I need is to find sub2_company1 and get the titles like a breadcrumb.
Level1 > sub2_company1
or if I search for item1_sub3_company1 I will get
Level1 > sub3_company1 > item1_sub3_company1
What I did so far but isn't working:
function breadcrumb($array, $needle) {
$path = array();
$array_iterator = new recursiveArrayIterator($array);
$it = new recursiveIteratorIterator($array_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach($it as $key => $value)
{
echo "$key: $value <br>";
if (!is_array($value)) {
array_push($path, $value);
}
if ($value === $needle) {
break;
}
}
//$content = '<div id="breadcrumb">' . implode(' > ', $path) . '</div>';
return print_r($path, 1);
}
thank you
You need a recursive function, not an iterative one.
The breadcrumb is inverted but you could use array_shift instead of push and you would have it the right way...