doucigua0278 2012-10-25 10:25
浏览 51
已采纳

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.

  • 写回答

2条回答 默认 最新

  • douan8473 2012-10-25 11:16
    关注

    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]
        ...
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图