Thread: Multi-dimensional arrays

Multi-dimensional arrays

From
"Hunter, Ray"
Date:

Is there any way to get the result from a query into a multi-dimensional array that can be done in a for loop.

 
 
 
 
RAY HUNTER
Automated Test Group
Software Support Engineer
 
ENTERASYS NETWORKS
 
Internal:     53888
Phone:       801 887-9888
Fax:          801 972-5789
Cellular:      801 698-0622
E-mail:       rhunter@enterasys.com
 
www.enterasys.com

RE: Multi-dimensional arrays

From
"tom"
Date:
for($I=0; $row=pg_fetch_array($result,$I,PGSQL_ASSOC); $I++){
    $resultArray[]=$row; // Results in a scalar array with nested associative arrays
}
return $resultArray;
 
 
You could also go a step further and manipulate each element of $row inside the for loop, with another loop:
 
for( ... ){
    while(list($key, $val)=each($row)){
        // play with each field element here...
    }
}
 
 
That said, my personal preference is to do it in a while loop instead of a for loop, I think there was some reason why PHP preferred this, although I can't remember why offhand.
 
 
$I=0;
while($I < pg_numrows($result)){
    $row=pg_fetch_array($result,$I,PGSQL_ASSOC);
    $resultArray[]=$row;
    $I++;
}
return $resultArray;
 
 
 
 
 
 -----Original Message-----
From: pgsql-php-owner@postgresql.org [mailto:pgsql-php-owner@postgresql.org]On Behalf Of Hunter, Ray
Sent: Wednesday, June 20, 2001 11:47 AM
To: Pgsql-php (E-mail)
Subject: [PHP] Multi-dimensional arrays

Is there any way to get the result from a query into a multi-dimensional array that can be done in a for loop.


 
 
 
RAY HUNTER
Automated Test Group
Software Support Engineer
 
ENTERASYS NETWORKS
 
Internal:     53888
Phone:       801 887-9888
Fax:          801 972-5789
Cellular:      801 698-0622
E-mail:       rhunter@enterasys.com
 
www.enterasys.com