Re: how to extract fields from associative array into different variables

From: Date: Tue, 16 Jun 2009 15:24:17 +0000
Subject: Re: how to extract fields from associative array into different variables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-294093@lists.php.net to get a copy of this message
Ford, Mike wrote: > On 15 June 2009 18:07, PJ advised: > > >> Is there an easier or simpler way to do this? >> code: >> >> $sql = "SELECT first_name, last_name, book_author.ordinal >> FROM author, book_author >> WHERE book_author.bookID = $idIN && book_author.authID = >> > author.id > >> ORDER BY ordinal"; >> >> $author = array(); >> if ( ( $results = mysql_query($sql, $db) ) !== false ) { >> while ( $row = mysql_fetch_array($results, MYSQL_ASSOC) ) { >> > > >> $author[] = $row; } >> } >> $numrows = mysql_num_rows($results); >> switch ($numrows) >> { >> case 5: >> $first_nameIN = $author[0]['first_name']; >> $last_nameIN = $author[0]['last_name']; >> $first_name2IN = $author[1]['first_name']; >> $last_name2IN = $author[1]['last_name']; >> $first_name3IN = $author[2]['first_name']; >> $last_name3IN = $author[2]['last_name']; >> $first_name4IN = $author[3]['first_name']; >> $last_name4IN = $author[3]['last_name']; >> $first_name5IN = $author[4]['first_name']; >> $last_name5IN = $author[4]['last_name']; >> break; >> case 4: >> $first_nameIN = $author[0]['first_name']; >> $last_nameIN = $author[0]['last_name']; >> snip.... >> > > Why not just use $author[0]['first_name'] instead of $first_nameIN (and > so on) in the rest of your code? Duh, I think I learned something here... it works! Thank you a zillion.... ;-) -- Hervé Kempf: "Pour sauver la planète, sortez du capitalisme." ------------------------------------------------------------- Phil Jourdan --- pj@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php

« previous php.general (#294093) next »