Posted by Ian Taylor on 12/28/06 19:44
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.
[Back to original message]
|