PHP:显示具有公共列约束的数组的值

I have a multidimensional array like this:

Array ( 

[0] => Array ( [ans_id] => 1 [ans_name] => PHP Hypertext Preprocessor [ques_id] => 1 [right_ans] => Yes [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP ) 

[1] => Array ( [ans_id] => 2 [ans_name] => Preety Home Page [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP ) 

[2] => Array ( [ans_id] => 3 [ans_name] => Programmed Hypertext Page [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP ) 

[3] => Array ( [ans_id] => 4 [ans_name] => Programmed Hypertext Preprocessor [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP ) ) 

My table is like this:

ans_id  ans_name                       ques_id                right_ans

1   PHP Hypertext Preprocessor           1                         Yes

2   Preety Home Page                     1                          No

3   Programmed Hypertext Page            1                          No

4   Programmed Hypertext Preprocessor    1                          No

5   Andy Suraski                        12                          No

6   Zeev Gutman                         12                          No

7   Rasmus Lerdorf                      12                         Yes

8   Perl                                12                          No

I want to retrieve the answers in sets of common question id's

i.e. one set of all answers for Ques_id='1', 2nd set of all answers for ques_id='12', etc.

I am supposed to use this code:

foreach($all_ans_cat as $r)
{                       
   echo $r['ans_id']."  ".$r['ans_name']."<br>";
}

This is retrieving all the values present in the table as one set and again in the 2nd set the same values are being displayed.

Select all answers sorted by ques_id and generate array with structure needed

$questions = array();
$prevQuestionId = null;
foreach ($all_ans_cat as $r) {
    if ($r['ques_id'] !== $prevQuestionId) {
        $questions[] = array(); 
    }
    $questions[sizeof($questions)-1][] = $r;
    $prevQuestionId = $r['ques_id'];
}

Result array would be:

0:
    - [ans1, ques1]
    - [ans2, ques1]
    ...
1: 
    - [ans5, ques13]
    - [ans6, ques13]
    ...
...

you can use a query with group by and aggregate function

have a look at

http://www.w3schools.com/sql/sql_groupby.asp