我想結合來自兩個單獨查詢的兩個數組。我將我的產品存儲在兩個獨立的表格中:一個用於一般信息,如名稱,說明,價格等;另一個用於它們的各種細節,即服裝物品的尺寸和顏色。從模型到控制器的組合和返回數組 - CodeIgniter
所以,我的數據庫的結構是這樣的產品:
products table:
p_id | title | descr | etc
product_attrs table:
attr_id | products.p_id | name | value
其中名稱和值可以是名稱=大小值=大
如果我試圖讓所有的細節的產品出來在一個查詢,例如:
this->db->select('products.title,
p.description,
p.price,
p.stock,
p.name,
p.value');
$this->db->from('p');
$this->db->where('p.p_id', $id);
$this->db->join('product_attrs', 'product_attrs.product_id = p.p_id', 'inner');
$result = $this->db->get();
return $result->result_array();
我得到填充名稱/值對有在該產品的表product_attributes數量的數組。因此,如果有說一個產品的5個屬性,我會得到一切回到5次這樣的:
Array ([0] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => Brand [value] => Modestly Active Swimwear) [1] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => Colour [value] => Black and Light Blue) [2] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => small) [3] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => medium) [4] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => large))
所以我決定將單獨的查詢每個表,這樣我就可以得到一個結果集的每個。但我想將它們結合起來,這樣我就可以將數據作爲一個數組返回給Controller,並將其顯示到我的視圖上。這就是我查詢的兩個表,我只是需要一種方法來結果結合起來,既:
$this->db->select('p.title,
p.description,
p.price,
p.stock');
$this->db->from('p');
$this->db->where('p_id', $id);
$result = $this->db->get();
$this->db->select('name, value');
$this->db->from('product_attrs');
$this->db->where('p_id', $id);
$result2 = $this->db->get();
如果有人能幫我會非常感激。謝謝
編輯:
我看array_merge()函數現在在php.net,但如果我這樣做:
$result = $this->db->get();
$array1 = $result->result_array();
$result2 = $this->db->get();
$array2 = $result2->result_array();
$data = array_merge($array1,$array2);
return $data;
我得到一個以上的陣列:
Array ([0] => Array ([title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20) [1] => Array ([name] => Brand [value] => Modestly Active Swimwear) [2] => Array ([name] => Colour [value] => Black and Light Blue) [3] => Array ([name] => size [value] => small) [4] => Array ([name] => size [value] => medium) [5] => Array ([name] => size [value] => large))
有沒有辦法在我的視圖中取出上述數組中的值?
我想我已經得到了它。我使用函數array_merge(),然後在我看來我可以得到的值如下:echo $ product_details [0] ['title'] ;.感謝上帝的var_dump! – a7omiton 2013-02-28 22:06:50
OP試圖編輯答案:表的前一個名稱是非常通用的,所以我沒有看到任何問題。你沒有編輯所有的事情嗎?你編輯你的問題,無論如何,但你仍然有'.description'問題並嘗試編輯'.desc'的答案。不會再匹配了。 – FelipeAls 2013-04-14 02:43:01
@FelipeAls我認爲這是重點:/。通用是一件好事嗎? – a7omiton 2013-04-14 04:23:04