note 13059 modified in function.array by vrana

From: Date: Tue, 17 Aug 2004 13:57:11 +0000
Subject: note 13059 modified in function.array by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80009@lists.php.net to get a copy of this message
Here's a cool tip for working with associative arrays- Here's what I was trying to accomplish: I wanted to hit a DB, and load the results into an associative array, since I only had key/value pairs returned. I loaded them into an array, because I wanted to manipulate the data further after the DB select, but I didn't want to hit the DB more than necessary. Here's how I did it: <?php //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"; } ?> I found this to be super efficient, and extremely cool. --was-- Here's a cool tip for working with associative arrays- Here's what I was trying to accomplish: I wanted to hit a DB, and load the results into an associative array, since I only had key/value pairs returned. I loaded them into an array, because I wanted to manipulate the data further after the DB select, but I didn't want to hit the DB more than necessary. Here's how I did it: //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"; } I found this to be super efficient, and extremely cool. http://php.net/manual/en/function.array.php

« previous php.notes (#80009) next »