note 13059 added to function.array.php

From: Date: Thu, 24 May 2001 15:12:01 +0000
Subject: note 13059 added to function.array.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-14250@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: //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://www.php.net/manual/en/function.array.php

« previous php.notes (#14250) next »