Re: extract varying data from array with different formatting

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

 



On Sat, 2009-04-11 at 16:11 -0500, PJ wrote:
> foreach does a nice job if you want the results identical each time.
> What can you use to change the formatting of the results dependent on
> the number of results.
> Here's an example:
> foreach ( $authors[$bookID] AS $authorID => $authorData ) {
>                 # Display the
>                 echo "{$authorData['first_name']} 
> {$authorData['last_name']}\n";
>             }
> 
> will echo - Joe Boe John Blue Andy Candy etc depending on how many rows
> we have.
> What I want is: Joe Boe, John Blue, Andy Candy & Hans Stick ( separated
> by commas, except for the last one which is separated with & .
> I thought of passing a variable to the foreach and then using if
> elseif... but that can't work because the variable is reset to 0 after
> each pass.
> Can't get switch to do it (maybe I don't understand it right.
> Help ?
> 
> -- 
> unheralded genius: "A clean desk is the sign of a dull mind. "
> -------------------------------------------------------------
> Phil Jourdan --- pj@xxxxxxxxxxxxx
>    http://www.ptahhotep.com
>    http://www.chiccantine.com/andypantry.php
> 
> 
$count = 1;
foreach ( $authors[$bookID] AS $authorID => $authorData )
{
  echo "{$authorData['first_name']} {$authorData['last_name']}\n";
  echo($count < count($authors[$bookID]))?', ':' & ';
  $count ++;
}


Ash
www.ashleysheridan.co.uk


-- 
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