我有一些數據,是的,數據。該數據來自MySQL查詢,並且始終包含4個項目。我想將這些數據緩存在一個數組表中以供稍後在網頁中使用,但我想保留查詢中的鍵並將每個分組分離到多維數組中。但是,爲了節省每次我想要查找給定數據組的迭代通過數組的時間,我想調用第一個數組的鍵與ID鍵相同,ID鍵始終是每個四項中的第一個鍵。如何使用陣列中的密鑰命名陣列密鑰
在分鐘,我用這個代碼:
function mysql_fetch_full_result_array($result)
{
$table_result=array();
$r=0;
while($row = mysql_fetch_assoc($result)){
$arr_row=array();
$c=0;
while ($c < mysql_num_fields($result)) {
$col = mysql_fetch_field($result, $c);
$arr_row[$col -> name] = $row[$col -> name];
$c++;
}
$table_result[$r] = $arr_row;
$r++;
}
return $table_result;
}
我目前正在測試這種使用3個獨特的用戶,所以我越來越從查詢三行回來,從這個函數結束數據達格式爲:
[0]=>
. . [id] => 1
. . [name] => random name
. . [tel] => random tel
. . [post] => post code data
[1]=>
. . [id] => 34
. . [name] => random name
. . [tel] => random tel
. . [post] => post code data
[2]=>
. . [id] => 56
. . [name] => random name
. . [tel] => random tel
. . [post] => post code data
那麼,如何改變代碼來代替鍵[0],[1],[2]給我的輸出:
[1]=>
. . [id] => 1
. . [name] => random name
. . [tel] => random tel
. . [post] => post code data
[34]=>
. . [id] => 34
. . [name] => random name
. . [tel] => random tel
. . [post] => post code data
[56]=>
. . [id] => 56
. . [name] => random name
. . [tel] => random tel
. . [post] => post code data
我不介意如果主數組鍵是數字串而不是數字,但我有點卡住了,我試着改變$table_result[$r] = $arr_row;
部分來讀$table_result[$result['id']] = $arr_row;
,但只輸出一個人的數組。我知道我需要另一個循環,但我正在努力研究如何編寫它。
奇妙的是,歡呼! – Confused 2010-03-19 14:59:48