我正在尝试使用 for 循环合并来自 mysql 的多个数组。但它不工作

I'm trying to merge multiple arrays from mysql using for loop. But it's not working

这是 MYSQL 中的数组。

这是我的 PHP 合并数组的代码。基本上我想验证用户输入的号码是否存在。

public function check_serial_from_exist(){
    $serial      = $this->input->post('serial_no_from');
    $scope_id    = $this->input->post('scope_id');       
    $client_id   = $this->input->post('client_id');       

    $get_serials = $this->db->select('used_serials')->where('certification_scope',$scope_id,'clientid',$client_id)->get('tbllabels')->result_array();
    $all_used_serials  = array();
    $status      = 'true';
    
    for($i=0;$i<count($get_serials);$i++){
        $all_used_serials[] = json_decode($get_serials[$i]['used_serials']);                
    }
    
    for($j=0;$j<count($all_used_serials);$j++){
       if(in_array($serial,$all_used_serials[$j])){
           $status = 'false';
       }
       else{
           $status = 'true';
       }
    }
    
    echo ''.$status;
}

当我打印数组时,我得到了这个:

Array ( [0] => Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 13 [4] => 14 [5] => 15 [6] => 16 [7] => 17 [8] => 18 ) [1] => Array ( [0] => 20 [1] => 21 [2] => 22 [3] => 23 [4] => 24 [5] => 25 [6] => 26 [7] => 27 [8] => 28 [9] => 29 [10] => 30 ) [2] => Array ( [0] => 50 [1] => 51 [2] => 52 [3] => 53 [4] => 54 [5] => 55 [6] => 56 [7] => 57 [8] => 58 [9] => 59 [10] => 60 ) [3] => Array ( [0] => 70 [1] => 71 [2] => 72 [3] => 73 [4] => 74 [5] => 75 [6] => 76 [7] => 77 [8] => 78 [9] => 79 [10] => 80 ) [4] => Array ( [0] => 85 [1] => 86 [2] => 87 [3] => 88 [4] => 89 [5] => 90 ) )

有人帮我解决这个问题吗?提前致谢。

在你的第二个 for 循环中,你将在每次迭代中覆盖 $status 的值,因此最终它被设置为最后一次迭代的结果。使用 array_merge 并取消第二个循环将解决此问题。

public function check_serial_from_exist() {
    $serial      = $this->input->post('serial_no_from');
    $scope_id    = $this->input->post('scope_id');       
    $client_id   = $this->input->post('client_id');       

    $get_serials = $this->db->select('used_serials')->where('certification_scope',$scope_id,'clientid',$client_id)->get('tbllabels')->result_array();
    $all_used_serials  = array();
    
    foreach($get_serials as $row) {
        $all_used_serials = array_merge(
            $all_used_serials,
            json_decode($row['used_serials'])
        );              
    }

    return in_array($serial, $all_used_serials)
}

在数据库中存储序列化数据很少是正确的解决方案,您应该考虑规范化这些数据。