RE: [PHP] how to extract fields from associative array into different variables
| From: | Yuri Yarlei | Date: | Thu, 18 Jun 2009 12:40:43 +0000 |
| Subject: | RE: [PHP] how to extract fields from associative array into different variables | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-294227@lists.php.net to get a copy of this message | ||
You can try use the foreach function.
ex:
if ($res = mysql_query ($sql, $db)) {
/* I personally prefer "res" for "resource", not "results" */
while ($r = mysql_fetch_assoc ($res)) {
$rows[] = $r;
}
}
foreach ($rows as $row){
echo $row['first_name'];
echo "<br>";
}
the foreach function transform the array $rows[] (the fisrt level of associative array) in a
variable $row.
Yuri Yarlei.
Programmer PHP, CSS, Java, PostregreSQL;
Today PHP, tomorrow Java, after the world.
Kyou wa PHP, ashita wa Java, sono ato sekai desu.
> To: php-general@lists.php.net
> From: news.NOSPAM.0ixbtqKe@luden.se
> Date: Thu, 18 Jun 2009 14:17:59 +0200
> Subject: Re: [PHP] how to extract fields from associative array into different variables
>
> On Tue, 16 Jun 2009 08:50:45 -0400, PJ wrote:
>
> > Ashley Sheridan wrote:
> >> On Mon, 2009-06-15 at 17:38 -0400, PJ wrote:
> >>
> >>> Then I have to add some loopy thing to assign the values to the
> >>> $variables... a real pita since my variable do not lend themselves too
> >>> well to linear alterations ($varIn, $var2IN, $var3IN... etc... so $i=0
> >>> and $i++ arren't going to be too cooperative here... I guess I'd have to
> >>> change that to $var1IN... and then figure out how to do $var.$i.IN .... I
> >>> already tried, but don't seem to have it right
> >>>
> >> Why do you need them in separate variables? Can't you use them using
> >> array syntax elsewhere in your code?
>
> > Basically, I don't know how else to handle inserting and updating random
> > ...
>
> What he's talking about is doing this:
>
> if ($res = mysql_query ($sql, $db)) {
> /* I personally prefer "res" for "resource", not "results" */
> while ($r = mysql_fetch_assoc ($res)) {
> $rows[] = $r;
> }
> }
>
> And then you access the individual rows/columns as
> $rows[0]['first_name']
> $rows[0]['last_name']
> $rows[1]['first_name']
> $rows[1]['last_name']
> and so on, in some kind of loop probably.
>
> (I thought this had been spelled out already, but I
> was unable to find it so perhaps not.)
>
>
> /Nisse
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
_________________________________________________________________
Deixe suas conversas mais divertidas. Baixe agora mesmo novos emoticons. É grátis!
http://specials.br.msn.com/ilovemessenger/pacotes.aspx