Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过循环和使用in_array()将两个数组与php进行比较?_Php_Codeigniter - Fatal编程技术网

如何通过循环和使用in_array()将两个数组与php进行比较?

如何通过循环和使用in_array()将两个数组与php进行比较?,php,codeigniter,Php,Codeigniter,以下是表格格式: 表1用户语言: id user_id lang_id status ------------------------------------- 1 20 1 1 2 21 1 1 3 21 2 0 4 21 3 1 表2语言:

以下是表格格式: 表1用户语言:

      id    user_id   lang_id   status
    -------------------------------------
      1      20         1        1
      2      21         1        1
      3      21         2        0
      4      21         3        1
表2语言:

     id    name
    ---------------
      1     English
      2     hindi
      3    Gujarati
      4    italian
型号代码:

     function select_user_language($user_id){$this->db->select('lang_id'); $this->db->from('user_language');$this->db->where('user_id ',$user_id); $this->db->where('status ',1); $query = $this->db->get();return $query->result(); }
     function  fins_status_user_lang($user_id,$lang_id){
       $this->db->select('*');
       $this->db->from('user_language');
       $this->db->where('user_id ',$user_id);
       $this->db->where('lang_id ',$lang_id);
       $this->db->where('status ',0);
       $query = $this->db->get();
       return $query->result();
    }
     function insert_user_lang($dta_sport){
       $this->db->insert('user_language',$dta_sport);
    }
    function change_status_user_lang($lang_id,$user_id,$status) {
    $sql = "UPDATE user_language SET status = '".$status."' WHERE lang_id = '".$lang_id."' and user_id='".$user_id."'";
    $query = $this->db->query($sql);
    if($query){
        return 1;
    } else {
        return 0;
    }
    }
控制器代码:

     $this->load->model('user/user_model');
    $user_id = $this->input->post('lang_id');
    $query1 = $this->user_model->select_user_language($user_id);
    $lang_var = $this->input->post('lang');//user selected language in array for example: Array ( [0] => 1 [1] => 2 [2] => 4 ) 
    $arr = array();
    if($lang_var != ""){
        $var1[] = "";
        $var2[] = "";
        foreach ($query1 as $row=>$value) {
            $lang_type = explode(',', $value->lang_id);
            foreach ($lang_var as $search) {
                if (in_array($search, $lang_type)) {
                 $query2 = $this->user_model->change_status_user_lang($search,$user_id,$status=1);
                }
                else {
                   $find = $this->user_model->fins_status_user_lang($user_id,$search);
                    if($find){
                        $query3 = $this->user_model->change_status_user_lang($search,$user_id,$status=1);
                    } else {
                        $dta_sport['user_id'] = $user_id;
                        $dta_sport['lang_id'] = $search;
                        $dta_sport['status'] = 1;
                        $this->user_model->insert_user_lang($dta_sport);
                    }
                }
            }
        }
我的问题是如何比较用户语言表数据(例如数组([0]=>stdClass对象([lang\u id]=>1)[1]=>stdClass对象([lang\u id]=>2)[2]=>stdClass对象([lang\u id]=>16)[3]=>stdClass对象([lang\u id]=>7])和用户选择的语言(例如数组([0]=>1[1]=>2[2]=>5[3]=>7[4]像这样。现在,如果用户选择的语言不是1,否则为0,我想更改user_language表的状态字段。或者用户选择用户语言表中不存在的新语言,而在用户语言表中创建新条目意味着插入该语言。
请给我一些快速解决问题的方法。

它不起作用吗?有什么问题?插入/更新?是。。插入更新是我的问题。如何将用户选择的值与表中已输入的值进行比较。。如果状态为零,如何更改状态?如果用户语言中没有语言,我如何插入新条目?为什么不使用
array\u diff()
?我尝试了array\u diff(),但没有给出正确的结果。它不起作用吗?有什么问题?插入/更新?是。。插入更新是我的问题。如何将用户选择的值与表中已输入的值进行比较。。如果状态为零,如何更改状态?如果用户语言中没有语言,如何插入新条目为什么不使用
array\u diff()
?我尝试了array\u diff(),但没有给出正确的结果。