Build Associative Array from database records

From: Date: Tue, 10 Oct 2000 19:07:44 +0000
Subject: Build Associative Array from database records
Groups: php.db 
Request: Send a blank email to php-db+get-3493@lists.php.net to get a copy of this message
I've been picking away at this all day and getting nowhere. I want to build an associative array from the results of a database query. This is the array I want to end up with, call it arClasses, with this for contents: "1"=>"Lake Lot", "2"=>"Ocean Lot", "3"=>"Forest Lot", "4"=>"Lake Island", "5"=>"Ocean Island", "6"=>"Luggage-luxury" so that I can ask for arClasses["4"] and have "Lake Island" returned, not "Ocean Lot", or "3". Here's what I have so far, database connection has been made .... $arClasses = array(); $sql = "select * from class"; $result = mysql_query( $sql ); while( $row = mysql_fetch_array( $result ) ) { $nClassKey = $row["nClassKey"]; $cClass = $row["cClass"]; array_push( $arClasses, $nClassKey, $cClass); } which returns this ... 0 => 1 1 => Lake Lot 2 => 2 3 => Ocean Lot 4 => 3 5 => Forest Lot 6 => 4 7 => Lake Island 8 => 5 9 => Ocean Island 10 => 6 11 => Luggage-luxury Not what I want. I tried creating a 2D array, by issuing $arClasses=array(); array_push($arClasses, array()); but that didn't get me anywhere. I've read the docs, search PHPFAQTS, but I've not had any luck. (And I reckon this is more a database than a PHP General question) Thanks for reading this far, and if you have a suggestion I'd welcome it. Miles Thompson

« previous php.db (#3493) next »