我想知道是否可以从 codeigniters 事件记录中合并如下所示的 SQL 查询。
//get assigned contacts
$this->db->select('*, CONCAT(first_name, " " ,last_name) AS name', FALSE);
$this->db->from('customers_contacts');
$this->db->join('customers_accounts', 'customers_accounts.contact_id = customers_contacts.contact_id');
$this->db->like('CONCAT(first_name, " " ,last_name)', $q);
$results1 = $this->db->get();
//get non assigned contacts
$this->db->select('*, CONCAT(first_name, " " ,last_name) AS name', FALSE);
$this->db->from('customers_contacts');
$this->db->like('CONCAT(first_name, " " ,last_name)', $q);
$results2 = $this->db->get();
我尝试使用 $query = array_merge($results1, $results2);
但这不起作用,我相信因为 ->get()
返回一个数组对象。
因此,我通过将两者都放入 foreach 循环,然后合并结果数组来使其工作。但我需要做一些条件,在一个 foreach 循环中比在两个 foreach 循环中更容易。
最佳答案
您可以像下面这样使用
$arr_results1 = $this->db->get()->result_array();
$arr_results2 = $this->db->get()->result_array();
var_dump(array_merge($arr_results1,$arr_results2));
希望这对您有帮助!
关于PhP(Codeigniter)在foreach循环之前合并查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32535014/