douhui1333 2016-05-04 13:55
浏览 53
已采纳

如何将从循环获得的值赋给数组

I want to assign or store the values I get from foreach loop into an array, so that I can use array sort function for the values. Please below is my code. It seems to create a whole new array for every loop with key = 0. And if i place the print statement below the loop, it will display only the last value obtained from the loop. I don't understand how it was explained in This Question

<?php  

  $studen_id = $this->db->get_where('student' , array('class_id' => $class_id))->result_array();
    foreach($studen_id as $row){
    $mark_obtained = $this->crud_model->get_exam_total($row2['exam_id'] , $class_id , $row['student_id']);

        $student_mark = array($mark_obtained);

        // rsort($student_mark);

        echo "<li>";  print_r($student_mark);  echo "</li>";

    }
?>

Output ARRAY

  • 写回答

5条回答 默认 最新

  • dte29947 2016-05-04 13:57
    关注

    You need to add [] to generate proper array. Try this:

    <?php  
      $studen_id = $this->db->get_where('student' , array('class_id' => $class_id))->result_array();
      $student_mark = array();
      foreach($studen_id as $row){
        $mark_obtained = $this->crud_model->get_exam_total($row2['exam_id'] , $class_id , $row['student_id']);
        $student_mark[] = $mark_obtained;
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大