我想要从两个单独的查询中组合两个数组。我将我的产品存放在两个独立的表格中:一个用于表示名称,描述,价格等一般信息;另外还有各种各样的细节,即服装的尺寸和颜色。
所以我的数据库的结构如下:
products table:
p_id | title | descr | etc
product_attrs table:
attr_id | products.p_id | name | value
其中name和value可以是name = Size value = Large
如果我尝试在一个查询中获取产品的所有详细信息,请执行以下操作:
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();
如果有人可以请求帮助,我会非常感激。谢谢
编辑:
我现在正在php.net中查看array_merge()函数,但如果我这样做:
$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 ) )
有没有办法在我的视图中从上面的数组中获取值?
答案 0 :(得分:2)
另一种方法是循环遍历第二个结果数组并将值添加到第一个数组。
// Get products results as an array
$this->db->select('products.title,
products.description,
products.price,
products.stock');
$this->db->from('products');
$this->db->where('product_id', $id);
$product = $this->db->get()->result_array();
// Get product attributes as an array
$this->db->select('name, value');
$this->db->from('product_attributes');
$this->db->where('product_id', $id);
$product_attributes = $this->db->get()->result_array();
// Loop through the results
foreach($product_attributes as $attribute) {
// Add results to original product array
$product[$attribute['name']] = $attribute['value'];
}
这应该产生一个这样的数组:
[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
[Brand] => Modestly Active Swimwear
[Colour] => Black and Light Blue
[size] => small