我不知道如何做到這一點,但如果可以做到這一點,任何人都可以幫助我。 基本上我有我的表3列:在數組中存儲數據庫內容PHP
ID Set Result Case
1 Set1 PASS 101
2 Set2 FAIL 102
3 Set2 FAIL 101
4 Set1 FAIL 101
5 Set1 PASS 104
$set = $row['Set'];
我所試圖實現的是,這些Set
的的foreach,存儲其關聯數組中的Result
和Case
。
我不知道如何做到這一點,但如果可以做到這一點,任何人都可以幫助我。 基本上我有我的表3列:在數組中存儲數據庫內容PHP
ID Set Result Case
1 Set1 PASS 101
2 Set2 FAIL 102
3 Set2 FAIL 101
4 Set1 FAIL 101
5 Set1 PASS 104
$set = $row['Set'];
我所試圖實現的是,這些Set
的的foreach,存儲其關聯數組中的Result
和Case
。
$arr = array();
foreach ($records as $key => $value)
{
$arr[$key]['Result'] = $value['Result'];
$arr[$key]['Case'] = $value['Case'];
}
echo '<pre>';
print_r($arr);
echo '</pre>';
在您的評論的光:
foreach ($records as $key => $value)
{
$arr[$key][$value['Result']] = $value['Case'];
}
在最近的評論的光:
foreach ($records as $key => $value)
{
$arr[$value['Set']][$value['Result']] = $value['Case'];
}
但是它是一個2維的aray,我認爲我需要一個3維數組。 [設置] [結果] [案例]? – JPro 2010-01-20 17:47:24
@JPro:我已經編輯它,再次檢查。 – 2010-01-20 17:50:09
這與他的表無關,$ arr與$最後幾乎相同。如果這是_is_他正在尋找的東西,我認爲它會更清楚一點,而不是設置()他不想要的數組元素('Set'和'ID') – 2010-01-20 17:52:05
閱讀評論後,我想我會採取裂縫它。
首先:除非您要檢查$array[Set2][{result}]
中的重複{result}鍵,並且在評論中存在重複的情況下將它們小寫,否則您要求的內容不起作用。我不知道爲什麼「 d做。這會令人困惑,並使我感到無聊。即:
$arr[Set2][FAIL]
與$arr[Set2][fail]
如果做到這一點,如圖上述[在阿利克斯Axel的第三代碼塊],你會做:
$arr[Set2][FAIL] = 102
然後覆蓋該數組索引值與$arr[Set2][FAIL] = 101
,導致你丟失數據。換句話說,您將「set」和「result」組合成一個「組合鍵」,可以這麼說,您不能這樣做,因爲這些組合不是唯一的(Set2 FAIL,Set2 FAIL )。我知道這是一個令人討厭的答案,但你應該看看你在做什麼,爲什麼,因爲我有一種預感,你會以錯誤的方式去做。你可能要像一個數組:
Array
(
[Set1] => Array
(
[101] => 'FAIL'
[102] => 'PASS'
)
[Set2] => Array
(
[101] => 'FAIL'
[102] => 'FAIL'
)
)
什麼的,但即使如此,當你有一些設置/案例對兩者通過和失敗,它不會工作。正因爲如此,你可以在這裏做的唯一事情就是使用「ID」作爲索引:
Array
(
[1] => Array
(
[Set] => 'Set1'
[Result] => 'PASS'
[Case] => '101'
)
[2] => Array
(
[Set] => 'Set1'
[Result] => 'FAIL'
[Case] => '101'
)
)
但我甚至不能告訴你該怎麼做,因爲你還沒有告訴我們如何你查詢結果數組是在第一個地方結構!所以第1步)請print_r或var_dump查詢結果。
+1替代方法。希望你現在不要低估我! :P – 2010-01-20 21:17:02
// assuming
$myArray = array();
$result = mysql_query("SELECT * FROM table ORDER BY Set ASC");
while ($rows = mysql_fetch_assoc($result)) {
for ($i = 0; $i <= count($rows); $i++) {
$myArray[$rows['ID']]['Set'] = $rows['Set'];
$myArray[$rows['ID']]['Result'] = $rows['Result'];
$myArray[$rows['ID']]['Case'] = $rows['Case'];
}
// output
Array
(
[1] => Array
(
[Set] => 'Set1'
[Result] => 'PASS'
[Case] => '101'
)
[2] => Array
(
[Set] => 'Set1'
[Result] => 'FAIL'
[Case] => '101'
)
[3] => Array
(
[Set] => 'Set1'
[Result] => 'PASS'
[Case] => '104'
)
[4] => Array
(
[Set] => 'Set2'
[Result] => 'FAIL'
[Case] => '102'
)
[5] => Array
(
[Set] => 'Set2'
[Result] => 'FAIL'
[Case] => '101'
)
)
年前寫了一個函數來處理返回MySQLs查詢結果的多維數組(名字與tablename相同)的短查詢。希望這會節省一些時間處理簡單和短期的MySQL查詢:
<?php
/*
FUNCTION: db2array($tablename, $condition)
Which simply makes a query: SELECT * FROM $tablename WHERE $condition
and returns multidimensional array()* named as
DB_table_name with keys named as tables_cell_name followed by row_number:
*Returns multidimensional array: $my_table[cell_name][row_number]
Example 1:
db2array("my_table", "cell_name = 'A' ORDER BY id LIMIT 1")
where "my_table" is DB tablename and $condition is all the rest
what comes after MySQL's WHERE;
Example 2:
db2array("my_table");
Returns: $my_table[];
To see results, go for print_r($my_table[]);
*/
function db2array($tablename, $condition){
global $$tablename;
//if $condition is NULL set it to 1=1
$condition = ($condition)?$condition:"1=1";
$query = mysql_query("
SELECT * FROM $tablename WHERE $condition
") or die(mysql_error());
// COUNTING FIELDS AND ROWS
$f_to = mysql_num_fields($query);
$n_to = mysql_num_rows($query);
// EXIT IF NO RESULTS
if (!mysql_num_rows($query)) return false;
// PUSHING ALL CELL NAMES TO $cellname ARRAY
for ($f=0; $f<=$f_to-1; $f++){
$cellname[$f] = mysql_field_name($query, $f);
}
// GENERATING OUTPUT ARRAY
for ($n=0; $n<=$n_to-1; $n++){
foreach ($cellname as $val){
$result[$val][$n] = mysql_result($query, $n, $val);
}
}
$$tablename = $result;
return $$tablename;
}
?>
是的,我使用MYSQL服務器 – JPro 2010-01-20 17:45:43
看起來像4列給我。 – Malfist 2010-01-20 17:46:05
你說你有三列,但你展示四個?另外,$行定義在哪裏?在$ row ['Set']中,是'Set'意爲一個文字,還是將它用作佔位符? – Dathan 2010-01-20 17:46:25