简体   繁体   中英

How to access 2D array fields in PHP

-----problem solved, see the update 2 below---- I put all MySQL query results into a 2D array:

$suppDescription=mysql_query($query);
$rows = mysql_num_rows($results);
$allSupplierInfo=array();

for($i=0; $i<$rows; $i++){
    $allSupplierInfo[]=mysql_fetch_row($suppDescription);
}

But now I cannot access the $allSuppliersInfo fields.

 echo $allSupplierInfo[1][1]; // prints out 'Array'
 echo $allSupplierInfo[1]['id'];  //prints out nothing

What am I doing wrong?

------- UPDATE-----

print_r($allSupplierInfo) prints the following, so it looks like the loop is not working as I wanted it to:

Array (
    [0] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) ) 
    [1] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) ) 
    [2] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) ) 
    [3] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) ) 
    [4] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] =>     Address_D [3] => Link_D ) [4] => Array ( [0] =>ID_E[1] => Name_E [2] => Address_E [3] => Address_E ) ) ) 

------ UPDATE 2----- Using the while loop, as suggested by RiggsFolly, solved the problem and I can access the fields as I initially wanted. I still do not understand why the for loop I used did not loop as I thought it would - any explanation would be greatly appreciated.

I think you got confused with your mysql result processing, you are using $result when checking for the number of rows returned but it should be $suppDescription .

This means that your for loop will not run as you will be getting 0 or probbaly FALSE as a response to mysql_num_rows($suppDescription);

$suppDescription=mysql_query($query);
$rows = mysql_num_rows($suppDescription);
$allSupplierInfo=array();

for($i=0; $i<$rows; $i++){
    $allSupplierInfo[]=mysql_fetch_row($suppDescription);
}

Also this is easier done with a while loop , then you just process whatever is returned and dont need to bother getting the number of rows.

$suppDescription=mysql_query($query);

$allSupplierInfo=array();

while ( $rows = mysql_fetch_row($suppDescription) ) {
    $allSupplierInfo[]=mysql_fetch_row($suppDescription);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM