将db内容存储在数组PHP中

I am not sure how to do this, but if it can be done can anyone please help me in this. Basically I have 3 columns in my table:

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'];

What I am trying to achieve is , foreach of these Set, store its associated Result and Case in an array.

$arr = array();

foreach ($records as $key => $value)
{
    $arr[$key]['Result'] = $value['Result'];
    $arr[$key]['Case'] = $value['Case'];
}

echo '<pre>';
print_r($arr);
echo '</pre>';

In light of your comment:

foreach ($records as $key => $value)
{
    $arr[$key][$value['Result']] = $value['Case'];
}

In light of your most recent comment:

foreach ($records as $key => $value)
{
    $arr[$value['Set']][$value['Result']] = $value['Case'];
}

After reading comments I thought I'd take a crack at it.

First of all: What you asking for will not work unless you are going to check for duplicate {result} keys in $array[Set2][{result}] and lowercase them if there are duplicates as in your comment, which I don't know why you'd do. It would be confusing and strikes me as nonsensical. To wit:

$arr[Set2][FAIL] vs. $arr[Set2][fail]

If you do it as shown above [in Alix Axel's third code block], you'll do:

$arr[Set2][FAIL] = 102 then overwrite that array index value with $arr[Set2][FAIL] = 101, causing you to lose data.

To put it another way, you are using a combination of the "set" and "result" as a "combined key" so to speak, which you CANNOT DO as the combinations are not unique (Set2 FAIL, Set2 FAIL). I know it's an annoying answer, but you should take a look at what you are doing and why, as I have a hunch you are going about it the wrong way. You probably want an array like:

Array
(  
    [Set1] => Array
    (  
            [101] => 'FAIL'  
            [102] => 'PASS'  
    )

    [Set2] => Array
    (  
            [101] => 'FAIL'  
            [102] => 'FAIL'  
    )
)

or something, but even then it won't work as you have some Set/Case pairs both passing and failing. Because of this, the only thing you can do here is use the "id" as an index:

Array
(  
    [1] => Array
    (  
            [Set] => 'Set1'  
            [Result] => 'PASS'  
            [Case] => '101'  
    )
    [2] => Array
    (  
            [Set] => 'Set1'  
            [Result] => 'FAIL'  
            [Case] => '101'  
    )
)

But I can't even tell you how to do that, cuz you haven't told us how your query results array is structured in the first place!! So step 1) Please print_r or var_dump the query results.

// 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'
    )
)

Ages ago written a function to handle short queries which returns multidimensional array (named same as tablename) of MySQLs query result. Hope this would save some time handling simple and short MySQL queries:

<?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;

}
?>