You are here: Re: Stop the commas!!! ( or knowing when you're at the end of an array ) « PHP Programming Language « IT news, forums, messages
Re: Stop the commas!!! ( or knowing when you're at the end of an array )

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация