Re: what to use instead of foreach
| From: | Mark Kelly | Date: | Mon, 13 Apr 2009 11:55:15 +0000 |
| Subject: | Re: what to use instead of foreach | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-291418@lists.php.net to get a copy of this message | ||
Hi.
On Sunday 12 April 2009, PJ wrote:
> foreach does not allow for different formatting for output...
[snip]
> But how do you get result1, result2 & result3 // with <br> at end ?
$lastIndex = count($a) - 1; // Adjust for zero-indexing.
$outputString = '';
foreach ($a as $index => $value) {
if ($index != $lastIndex) {
$outputString .= "$value, ";
} else {
$outputString = rtrim($outputString,', '); // Strip last comma.
$outputString .= " & $value<br />";
}
}
echo $outputString;
Like that? If your array is associative you can drop the $lastIndex calc
and adjust the loop to update a counter instead.
HTH
Mark