Re: Stop the commas!!! ( or knowing when you're at the end of an array )
- From: Ian Taylor <mx3@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Dec 2006 19:44:48 +0000
phpCodeHead wrote:
Hi all,
I am looking for a good way to know when I am at the end of a
comma-separated list of array values.
My code is simple (as well as I'm sure is the solution I'm looking for;
and YES I've search the php manual!! Arrggghh! )
Anyway the code goes like this:
...
<td><select name="pref_cities[]" multiple size="6">
<option>Nagpur</option>
<option>Mumbai</option>
<option>Bangalore</option>
<option>Chennai</option>
<option>Kolkatta</option>
</select></td>
...
<?php
$cities = $_POST['pref_cities'];
foreach ($cities as $city) {
echo $city . ", ";
}
?>
I want to know when I'm at the last element in $cities[] so that I can
output a period (.) instead of a comma (,).
Thanx all! Have a HAPPY NEW YEAR!!
Gene
I'd take a slightly different approach:
echo implode(", ", $cities).".";
HTH
Ian.
.
- References:
- Stop the commas!!! ( or knowing when you're at the end of an array )
- From: phpCodeHead
- Stop the commas!!! ( or knowing when you're at the end of an array )
- Prev by Date: Stop the commas!!! ( or knowing when you're at the end of an array )
- Next by Date: I cannot install anything with pear.
- Previous by thread: Stop the commas!!! ( or knowing when you're at the end of an array )
- Next by thread: Re: Stop the commas!!! ( or knowing when you're at the end of an array )
- Index(es):
Relevant Pages
|