douewei1665
2018-08-14 14:28 阅读 39
已采纳

如果使用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 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);
    
    点赞 评论 复制链接分享
  • dongnaoben4456 dongnaoben4456 2018-08-14 14:39

    I flushed out the entire array for $stu_result. Is this what you are looking for?

    <?php
    $stu_result = [
      array("exam_type" => 1, "subject_id" => 5,  "converted_mark" => 5.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 6,  "converted_mark" => 4.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 7,  "converted_mark" => 5.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 8,  "converted_mark" => 3.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 9,  "converted_mark" => 4.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 10, "converted_mark" => 3.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 11, "converted_mark" => 4.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 12, "converted_mark" => 5.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 13, "converted_mark" => 4.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 14, "converted_mark" => 5.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 15, "converted_mark" => 2.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 1, "subject_id" => 16, "converted_mark" => 1.00, "student_id" => 186, "sub_name" => "maths"),
      array("exam_type" => 2, "subject_id" => 17, "converted_mark" => 5.00, "student_id" => 186, "sub_name" => "not maths"),
      array("exam_type" => 2, "subject_id" => 18, "converted_mark" => 2.00, "student_id" => 186, "sub_name" => "not maths"),
      array("exam_type" => 2, "subject_id" => 19, "converted_mark" => 5.00, "student_id" => 186, "sub_name" => "not maths"),
    ];
    $result_exam1 = array();
    foreach($stu_result as $result_temp){
      if($result_temp['exam_type'] == 1){
        $row = array( //create an array, with two values and map the values
          "converted_mark" => $result_temp['converted_mark'],
          "sub_name"  => $result_temp['sub_name']
        );
        $result_exam1[] = $row; //append to the array
      }
    }
    var_dump($result_exam1);
    ?>

    </div>
    
    点赞 评论 复制链接分享

相关推荐