Associative Array by Column Name

Submitted by:David Villa

Date added:12 January, 2012

Category:PHP

Here shown a php code example how to associative Array by Column Name

Tags: associative array

Code Snippet:

<?php 
$result_num = mysql_list_fields($dbName,$tab);
$count = mysql_num_fields($result_num);

//build array of column names
for($j = 0; $j < $count; $j++){
$name = mysql_field_name($result_num,$j);
$fieldArr[$j] = $name;
}


$result_num1 = mysql_query($qStr);
if($result_num1){
$numRowsFound = mysql_num_rows($result_num1);
}
else {
//ERROR CONDITION
}

for($i = 0,$result = mysql_fetch_row($result_num1);$i < $numRowsFound;$result = mysql_fetch_row($result_num1),$i++) {
for($j = 0; $j < $count; $j++){
$memArr[$i][$fieldArr[$j]] = $result[$j];
}
}
?>
 
 

Comments