循环过程仅显示每次迭代的第一个结果[重复]

问题描述 投票:0回答:2

在我的 CodeIgniter 应用程序中,我收到一组应该签入的学生录取号码。

入场号码可通过

$ad_no = $this->input->post('adn_no');
访问,可能如下所示:

array(5) {
    [0]=> string(5) "11784"
    [1]=> string(5) "11837"
    [2]=> string(5) "11775"
    [3]=> string(5) "11937"
    [4]=> string(5) "12061"
}

我尝试用这些数字查询数据库,但结果仅显示第一个学生的结果。

我的代码:

foreach ($ad_no as $key => $id) {
    $this->db->where_in('ad_no', $id);
    $query = $this->db->get('duration');
    $r = $query->result_array();

    $dur = $r[0]['tot_hr'];
    $start = explode(':', $dur);
    $end = explode(':', $tm);
    $total_hours = $start[0] - $end[0];
    $total_hours1 = $start[1] - $end[1];
    $mins = abs($total_hours1);
    $dur = $total_hours . ":" . $mins;
    $durs[$key] = array(
        'ad_no' => $ad_no[$key],
        'tot_hr' => $dur
    );
}
$reslt = $this->Daily_temp_model->duration($durs);
arrays loops codeigniter
2个回答
1
投票

在 foreach 循环中使用数组推送

$result=[];
foreach($ad_no as $key => $id) {
    $this->db->where_in('ad_no', $id);
    $query = $this-> db-> get('duration');
    $r = $query-> result_array();
    array_push($result, $r);
    $dur = $r[0]['tot_hr'];
    $start = explode(':', $dur);
    $end = explode(':', $tm);
    $total_hours = $start[0] - $end[0];
    $total_hours1 = $start[1] - $end[1];
    $mins = abs($total_hours1);
    $dur = $total_hours.":".$mins;
    $durs[$key] =
        array(
            'ad_no' => $ad_no[$key],
            'tot_hr' => $dur
        );
}
$reslt = $this->Daily_temp_model->duration($durs);

1
投票

使用 in_array

<?php
$a = array('1.10', 12.4, 1.13);

if (in_array('12.4', $a, true)) {
    echo "'12.4' found with strict check\n";
}

if (in_array(1.13, $a, true)) {
    echo "1.13 found with strict check\n";
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.