RE: [PHP] Newbie array problem

From: Date: Thu, 23 Sep 2004 14:13:25 +0000
Subject: RE: [PHP] Newbie array problem
Groups: php.general 
Request: Send a blank email to php-general+get-197680@lists.php.net to get a copy of this message
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:kevin@oceania.net] > Sent: Thursday, September 23, 2004 6:34 AM > To: php-general@lists.php.net > Subject: Re: [PHP] Newbie array problem > > > This one time, at band camp, "Phpu" <phpu@go.ro> 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

« previous php.general (#197680) next »