簡體   English   中英

如果使用foreach條件為真,如何在空白數組中獲取數組數據

[英]how to get array's data in a blank array if condition is true using foreach

我有一個數組:

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

希望這個能對您有所幫助 :

注意: $stu_result應該是要循環的多維數組,並使用foreach循環的key value功能獲取所有數據,如下所示:

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

我用$ 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); ?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM