I definitely think implode() is the better way to do this, but if you
DID have to loop through an array, isn't it easier to do "Foreach
($array as $item)"?

In this case, requiring the ";" between them (and choosing not to use
implode) you could do something like this:

Foreach ($array as $count=>$item){
  if ($count == 0) {
    $display = $item;
  } else {
    $display = ";$item";
  }
}


If you were using an associative array, you could use a $i type counter
to determine if you're at the first element or not.

Much less code and I never saw the point in using a "while" loop for
arrays.

Just something to think about.  Both ways work fine and as much as
things come down to style, they are also equally weighted by what the
programmer is familiar with and confortable with.

-TG


> -----Original Message-----
> From: Kevin Waterson [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, September 23, 2004 6:34 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [PHP] Newbie array problem
> 
> 
> This one time, at band camp, "Phpu" <[EMAIL PROTECTED]> wrote:
> 
> > If i have an array
> > $array = array(1, 2, 3, 4, 5, 6, 7)
> > How can i display the array element sepparated by (;) like this
> > $display = 1;2;3;4;5;6;7
> 
> $i=0;
> 
> $arraySize = sizeof($array);
> 
> while($i < $arraySize){
>   echo $array[$i];
>   if($i < ($arraySize-1)){
>      echo ';';
>    }
>   $i++;
> }
> 
> Kevin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to