(Beginner's question) Echoing through an array in PHP, only adding a comma based on certain conditions
So this is pretty basic, I know! Sorry, just one of those days.
I've got an array of tags collected from a database. There could be any number of tags in this array. However, I only want to output 4.
So I currently have this code:
$iterat开发者_如何学运维ion = 0;
foreach ($tagarray as $tag) { ?>
<div class="tagbutton listv">
<span><?php echo $tag; ?></span>,
</div>
<?php
$iteration++;
if ($iteration == 4) {
break;
}
} ?>
You'll see after </span>
there's a ,
comma. Obviously this looks weird if the output looks like this:
tag1, tag2, tag3,
With a trailing comma. So I thought I could put this where the comma currently is:
<?php if ($iteration < count($tagarray) {echo ",";} ?>
That works, but, only when the count of $tagarray
is greater than 4 or something. And, like I say, $tagarray could be any value.
I also tried
<?php if ($iteration == 0 || $iteration == 1 || $iteration == 2 || $iteration == 3) {echo ",";}?>
Which, although a bit repetitive, should work, doesn't, because $tagarray could contain 2 tags, and therefore still have a trailing comma.
I realise this is probably a simple one, but hey, I really appreciate the help!
Thanks!
Jack
You can buffer your output (the tags). Let's say you put them in an array
array_push($myTags, '<span>'.$tag.'</span>');
When you want to show them you just implode(",", $myTags)
.
Or you can buffer them in a string and then substr()
on that string until you get rid of the last comma.
Personally, I'd suggest the array.
Using implode is better for outputting stuff like this to a string, and array_slice can be used to return the first four elements. Assuming your array is called tagarray:
echo "<span>" . implode("</span>,<span>", array_slice($tagarray,0,4)) . "</span>";
Obviously changing the "glue" to what you want. This negates the need for a foreach loop and may be faster.
I would separate the concerns of slicing 4 elements, transforming each element, and joining the transformed results together. This would render your code more readable.
// take 4 elements
$firstelements=array_slice( $tags, 0, 4 );
// how to transform an element
function tag_span($tag) {
return "<span>$tag</span>";
}
// transform all needed elements
$spans=array_map( tag_span, $firstelements );
// join them together
$result=implode( ", ", $spans );
// output the result
echo $result;
If you want to do it manually, just prepend the comma before any tag but the first one. Use a control variable to detect the first tag.
$iteration = 0;
$first=true;
foreach ($tagarray as $tag) { ?>
<div class="tagbutton listv">
<span><?php
if($first){$first=false;}
else{echo ",";}
echo $tag;
?></span>
</div>
<?php
$iteration++;
if ($iteration == 4) {
break;
}
} ?>
If you want to use library functions, use implode
as suggested by Cetra and Bogdan.
Solution based on your code would be
$iteration = 0;
$processarr = array_slice($tagarray,0,4); //get tags you need,4 or less
foreach ($processarr as $tag) { ?>
<div class="tagbutton listv">
<span><?php echo $tag; ?></span>,
</div>
<?php
$iteration++;
if($iteration < count($processarr)) echo ",";
}?>
精彩评论