Re: what to use instead of foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux