0

我有一个数组:

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

它的长度是 15。
我想匹配考试类型并将 sub_name 和转换后的分数存储在一个空白数组中。我试过这段代码:

这是我到目前为止尝试过的代码

$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'];
        }
}
4

2 回答 2

2

希望对你有帮助 :

注意:$stu_result 应该是一个多维数组来循环并使用key valueforeach 循环的功能来获取所有数据,如下所示:

$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);
于 2018-08-14T14:32:46.647 回答
1

我为 $stu_result 刷新了整个数组。这是你想要的?

<?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);
?>

于 2018-08-14T14:39:03.780 回答