Hi
I wanted to load the result of an SQL query into an array for further manipulation. I got this code on the php.net documentation. The code is fine but I'm having problems understanding it's logic - I'm new to Php, by the way.
May be one of you could help me?
********code below*************
//assume db connectivity
//load it all into the associative array
$sql = "SELECT key,value FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)) {
$myArray[$row[0]] = $row[1];
}
//now we expand it
while(list($key,$value) = each($myArray)) {
echo "$key : $value";
}
********code above*************
I'm having a very hard time understanding the line:
myArray[$row[0]] = $row[1]
I understand that row[0] will contain the name of the very first field
retrieved from the database and that row[1] will contain the next one.
But I cannot figure out what exactly the above line is supposed to do:
why is the next fieldname(i.e. $row[1])assigned to an element of
myArray whose index is the previous fieldname?
May be the answer is simple but I really cannot figure it out and it seems noone to
whom I've asked can.
I would be grateful if anyone of you could help me out.
regards
John