我如何比较两个数组与php循环和使用in_array()?

时间:2016-10-18 10:35:10

标签: php codeigniter

以下是表格格式: table1 user_language:

      id    user_id   lang_id   status
    -------------------------------------
      1      20         1        1
      2      21         1        1
      3      21         2        0
      4      21         3        1

table2语言:

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

我的问题是如何比较user_language表数据(例如Array([0] => stdClass Object([lang_id] => 1)[1] => stdClass Object([lang_id] => 2)[2] => stdClass对象([lang_id] => 16)[3] => stdClass对象([lang_id] => 7)))像这样和用户选择的语言(例如Array([ 0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 16))。现在我想更改user_language表的状态字段,如果用户选择的语言不是1,否则为0.或者用户选择user_language表中不存在的新语言而不是在user_language表中创建新条目意味着插入该语言。 请给我一些解决我问题的快速解决方案。

0 个答案:

没有答案