0

I have an array, which I am using the following code:

    foreach ($taglist as $tag=>$size){
      echo link_to(
          $tag, 
          "@search-tag?tag=" . strtolower($tag), 
          array(
              "class"  => 'tag' . $size, 
               "title" => "View all articles tagged '" . $tag . "'"
           )
        );
    }

Now, this simply prints a hyperlink

What I'm looking to do, is to add the pipe char ( | ) after every link, apart from the last one.

Could I do this in a loop?

Thanks

Gordon
  • 312,688
  • 75
  • 539
  • 559
terrid25
  • 1,926
  • 8
  • 46
  • 87
  • possible duplicate of [Peek ahead when iterating an array in PHP 5.2](http://stackoverflow.com/questions/2458099/peek-ahead-when-iterating-an-array-in-php-5-2) – Gordon Jan 26 '11 at 12:44
  • possible duplicate of [adding string to all but last item in array](http://stackoverflow.com/questions/4804399/adding-string-to-all-but-last-item-in-array) – Mark Baker Jan 26 '11 at 12:56

4 Answers4

5
$k = 0;
foreach($taglist as $tag=>$size)
{
    $k++;
    echo link_to($tage, ...);
    if ($k != sizeof($taglist)) echo '|';
}
Simon
  • 22,637
  • 36
  • 92
  • 121
1

You can use a plain old boolean variable:

$first = true;
foreach($taglist as $tag=>$size){
    if ($first) $first = false; else echo '|';
    echo link_to($tage, ...);
}

Note that technically, this code outputs a bar before every element except the first, which has the exact same effect as outputting a bar after every element except the last.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • That would print only before the first one, not all except last. – Dogbert Jan 26 '11 at 12:39
  • He wants to skip that output on the last iteration, not the first. – Dan Grossman Jan 26 '11 at 12:39
  • @Dogbert @Dan Grossman Sorry, I do not understand your comments. When I replace the foreach with a loop through numbers, the result looks like 0|1|2|3|4|5|6|7|8|9 which is pretty much what he wanted, isn't it? – phihag Jan 26 '11 at 12:43
1

Use a temporary array then join elements /

$links = array();
foreach($taglist as $tag=>$size){
    $links[] = link_to($tag, ...);
}
echo implode('|', $links);
phihag
  • 278,196
  • 72
  • 453
  • 469
Toto
  • 89,455
  • 62
  • 89
  • 125
1

You can use a CachingIterator

$links = new CachingIterator(new ArrayIterator($tagList));
foreach($links as $tag => $size) {
    echo link_to(/* bla */), $links->hasNext() ? '|' : '';
}

For more info on the CachingIterator see my answer at Peek ahead when iterating an array in PHP

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • How would I use that to add the | to all but the last 4? Thanks – terrid25 Jan 26 '11 at 14:43
  • @terrid25 not possible without the use of a helper variable that compares how often you iterated so far against the total array size. If you need that, [Syom's solution](http://stackoverflow.com/questions/4804599/adding-a-char-to-all-array-items-ap-art-from-last-using-for-foreach/4804716#4804716) is easier to modify because it already does that to determine whether you are on the last item. – Gordon Jan 26 '11 at 14:50
  • Ok, I'll have a look at his solution and see if i can modify it in some way... Thanks – terrid25 Jan 26 '11 at 15:20