我想知道是否有可能從codeigniters活動記錄中合併如下所示的sql查詢。PhP(Codeigniter)在foreach循環之前合併查詢結果
//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循環中比兩個循環更容易的條件。
對不起,我本來應該更清楚,因爲這兩個查詢都在同一個表中,只有一個與聯結表連接(已分配與未分配的聯繫人),分配的聯繫人中會返回額外的列。所以工會不行。 – skribe
我認爲你需要這個:http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array –