douewei1665 2018-08-14 14:28
浏览 97
已采纳

如果使用foreach条件为真,如何在空数组中获取数组的数据

I have an array :

$stu_result( [exam_type] => 1 [subject_id] => 5 [converted_mark] =>5.00[student_id] => 186 [sub_name] => maths)

and its length is 15.
I want to match exam type and store sub_name and converted marks in a blank array. I tried this code :

This is my code tried so far

$result_exam1 = array();
foreach($stu_result as $result_temp){
 if($result_temp['exam_type'] == 1){
            $result_exam1['converted_mark'] = $result_temp['converted_mark'];
            $result_exam1['sub_name'] = $result_temp['sub_name'];
        }
}
  • 写回答

2条回答 默认 最新

  • dsxxqndv41105 2018-08-14 14:32
    关注

    Hope this will help you :

    Note : $stu_result should be a multidimensional array to loop and use key value feature of foreach loop to get all data like this :

    $result_exam1 = array();
    foreach($stu_result as $key => $result_temp)
    {
       if($result_temp['exam_type'] == 1)
       {
           $result_exam1[$key]['converted_mark'] = $result_temp['converted_mark'];
           $result_exam1[$key]['sub_name'] = $result_temp['sub_name'];
       }
    }
    print_r($result_exam1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题