$cats=array($cat1, $cat2, $cat3, $cat4, $cat5); foreach($category as $index => $value) { if(in_array($index, $cats)) { $output[]= '<a href="/directory/'.$index.'/">'.$value[2].'</a>'; foreach($subcats as $index1 => $value1) { if($value1[0] == $index) { $output[]= $value1[2]; } } } } $output = implode(' | ',$output); echo $output; Code (markup): I want the INNER foreach loop to append it's value to the current element of the $output[] array, rather than adding a new element.
Basically what I'm trying to acheive is to not have the 'pipe' symbol appear when the inner foreach loop is run - if I can get the value appended to the current element of the array then it isn't going to display it.
$cats=array($cat1, $cat2, $cat3, $cat4, $cat5); $i = 0; foreach($category as $index => $value) { if(in_array($index, $cats)) { $output[$i]= '<a href="/directory/'.$index.'/">'.$value[2].'</a>'; foreach($subcats as $index1 => $value1) { if($value1[0] == $index) { $output[$i] .= $value1[2]; } } $i++; } } $output = implode(' | ',$output); echo $output; PHP: