从数组中删除重复值
Posted
技术标签:
【中文标题】从数组中删除重复值【英文标题】:Removes duplicate values from an array 【发布时间】:2014-10-10 09:38:39 【问题描述】:我有一个使用 mysql 的 CodeIgniter 生成的数组。 [co_name] =>
中的值是重复的,我尝试使用 co_id
对它进行 group_by
它不起作用,但它也使用 group_by
使用 group_by
.CodeIgniter group_by
仅适用于一个 group_by
或任何从[co_name] =>
删除重复值的其他方法。
Array
(
[0] => stdClass Object
(
[order_date] => 1408255199
[cus_id] => 6
[order_deliver_name] => Jankia
[order_deliver_add_no] => 5085
[order_deliver_add_street] => mapanawathura road
[order_deliver_add_city] => Kandy
[order_deliver_contact] => 0716352632
[order_deliver_date] => 2014-08-12
[order_status] => pending
[emp_id] =>
[cmp_name] => Burger Bun
[co_name] => Put Ham
Put Tomato
Put Ham
Put Tomato
Put Chilli Sauce
Put Tomato
Put Chilli Sauce
Put Ham
Put Chilli Sauce
[order_id] => 28
[quantity] => 10
)
这是我的代码
function get_customize_orders()
$this->db->select(array(
'tbl_order_product_details.order_date',
'tbl_order_product_details.cus_id',
'tbl_order_product_details.order_deliver_name',
'tbl_order_product_details.order_deliver_add_no',
'tbl_order_product_details.order_deliver_add_street',
'tbl_order_product_details.order_deliver_add_city',
'tbl_order_product_details.order_deliver_contact',
'tbl_order_product_details.order_deliver_date',
'tbl_order_product_details.order_status',
'tbl_order_product_details.emp_id',
'tbl_customise_main_product.cmp_name',
'GROUP_CONCAT(tbl_customise_optional.co_name SEPARATOR "<br />" ) as co_name ',
'tbl_order_products.order_id',
'tbl_order_products.quantity',
));
$this->db->from('tbl_order_products');
$this->db->join('tbl_customise_join_products', 'tbl_customise_join_products.cmp_id=tbl_order_products.product_id');
$this->db->join('tbl_customise_main_product', 'tbl_customise_main_product.cmp_id=tbl_customise_join_products.cmp_id');
$this->db->join('tbl_customise_order_products', 'tbl_customise_order_products.order_id=tbl_order_products.order_id');
$this->db->join('tbl_customise_optional', 'tbl_customise_optional.co_id=tbl_customise_order_products.co_id');
$this->db->join('tbl_order_product_details', 'tbl_order_product_details.order_id=tbl_order_products.order_id');
$this->db->group_by('tbl_order_products.order_id');
$query = $this->db->get();
return $query->result();
【问题讨论】:
【参考方案1】:在GROUP_CONCAT
中使用DISTINCT
,这样它只会连接每个order_id
组中不同的co_name
GROUP_CONCAT(DISTINCT tbl_customise_optional.co_name SEPARATOR "<br />" ) as co_name
【讨论】:
以上是关于从数组中删除重复值的主要内容,如果未能解决你的问题,请参考以下文章