bloodgoat Posted March 26, 2009 Share Posted March 26, 2009 Say this is my navigation array: <?php $navigation = array(); $navigation[] = array("Page 1", "URL 1"); $navigation[] = array("Page 2", "URL 2"); $navigation[] = array("Page 3", "URL 3"); ?> Got it? Awesome. Now suppose I'm using a foreach() loop to display them. <?php foreach($navigation as $link){ $links .= "<a href=\"".$link[1]."\">$link[0]</a> "; } ?> Alright, now when I post "$links" on my page, it displays them as: Page 1 Page 2 Page 3 It's legible if you're not a moron, but still... I think it would look a bit nicer if it looked something like this: Page 1 | Page 2 | Page 3 I realize I could separate them by looping them with <td> and putting them each in their own column, but I don't have THAT much room to work with in the column they're being inserted to, and I don't want to stretch out my page or anything. So how do I do what I mentioned above? Obviously I could just loop them like $links .= "<a href=\"".$link[1]."\">$link[0]</a> | "; but that means even the last link would have the stick at the end which just isn't acceptable. Enlighten me? Quote Link to comment https://forums.phpfreaks.com/topic/151276-solved-separate-looped-foreach-variables-with-special-characters/ Share on other sites More sharing options...
lonewolf217 Posted March 26, 2009 Share Posted March 26, 2009 you can use array_count to get the total number of items in the array, then a counter to see which index you are on to determine whether or not to output the separator Quote Link to comment https://forums.phpfreaks.com/topic/151276-solved-separate-looped-foreach-variables-with-special-characters/#findComment-794650 Share on other sites More sharing options...
KPH71 Posted March 26, 2009 Share Posted March 26, 2009 This will work: $k = 1; foreach($navigation as $link){ if ($k!=1){$links .= ' | ';} $links .= "<a href=\"".$link[1]."\">$link[0]</a>"; $k++; } Basically all of the ones except the first in the loop will output | before them Quote Link to comment https://forums.phpfreaks.com/topic/151276-solved-separate-looped-foreach-variables-with-special-characters/#findComment-794651 Share on other sites More sharing options...
wildteen88 Posted March 26, 2009 Share Posted March 26, 2009 When you echo out $links, remove the last 3 characters from the string with substr, example foreach($navigation as $link){ $links .= "<a href=\"".$link[1]."\">$link[0]</a> | "; } $links = substr($lines, 0, -3); echo $links; Quote Link to comment https://forums.phpfreaks.com/topic/151276-solved-separate-looped-foreach-variables-with-special-characters/#findComment-794652 Share on other sites More sharing options...
bloodgoat Posted March 26, 2009 Author Share Posted March 26, 2009 Excellent, that was easier than I thought it would be. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/151276-solved-separate-looped-foreach-variables-with-special-characters/#findComment-794654 Share on other sites More sharing options...
steelaz Posted March 26, 2009 Share Posted March 26, 2009 One more: <?php foreach($navigation as $link){ $array[] = "<a href=\"".$link[1]."\">$link[0]</a> "; } echo implode(' | ', $array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151276-solved-separate-looped-foreach-variables-with-special-characters/#findComment-794656 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.