我不知道该怎么做,但如果可以做到,任何人都可以帮助我。 基本上我的表中有3列:
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
,将其关联的Result
和Case
存储在数组中。
答案 0 :(得分:2)
$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'];
}
答案 1 :(得分:2)
看完评论后,我以为我会对它进行抨击。
首先:除非您要在$array[Set2][{result}]
中检查重复的{结果}键,否则您要求的内容将无效,如果您的评论中有重复项,则会将其缩小,我不知道为什么你这样做。这将是令人困惑的,并且让我觉得荒谬。即:
$arr[Set2][FAIL]
与$arr[Set2][fail]
如果您按照上面[Alix 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'
)
)
或其他东西,但即使这样它也不会起作用,因为你有一些Set / Case对传递和失败。因此,你在这里唯一能做的就是使用“id”作为索引:
Array
(
[1] => Array
(
[Set] => 'Set1'
[Result] => 'PASS'
[Case] => '101'
)
[2] => Array
(
[Set] => 'Set1'
[Result] => 'FAIL'
[Case] => '101'
)
)
但我甚至不能告诉你如何做到这一点,因为你没有告诉我们你的查询结果数组是如何构建的!所以步骤1)请print_r或var_dump查询结果。
答案 2 :(得分:1)
// 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'
)
)
答案 3 :(得分:0)
Ages之前编写了一个处理短查询的函数,它返回MySQL查询结果的多维数组(命名为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;
}
?>