gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:40 24 4
gpt4 key购买 nike

我有一个数组:

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

关于php - 如果条件为真,如何使用foreach在空白数组中获取数组的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51843820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com