我与阵列结果挣扎:如何修整数组以仅显示非重复值?
$result = Db::getInstance()->ExecuteS($sql);
$this->smarty->assign(array(
'result' => $result
));
这是我的数组:
Array (
[0] => Array ([id_tag] => 2 [id_post] => 5 [id_smart_blog_post_shop] => 5 [id_smart_blog_post] => 5 [id_shop] => 1 [id_lang] => 1 [name] => Microchip)
[1] => Array ([id_tag] => 2 [id_post] => 6 [id_smart_blog_post_shop] => 6 [id_smart_blog_post] => 6 [id_shop] => 1 [id_lang] => 1 [name] => Microchip)
[2] => Array ([id_tag] => 2 [id_post] => 7 [id_smart_blog_post_shop] => 7 [id_smart_blog_post] => 7 [id_shop] => 1 [id_lang] => 1 [name] => Microchip)
[3] => Array ([id_tag] => 2 [id_post] => 8 [id_smart_blog_post_shop] => 8 [id_smart_blog_post] => 8 [id_shop] => 1 [id_lang] => 1 [name] => Microchip)
[4] => Array ([id_tag] => 2 [id_post] => 9 [id_smart_blog_post_shop] => 9 [id_smart_blog_post] => 9 [id_shop] => 1 [id_lang] => 1 [name] => Microchip)
[5] => Array ([id_tag] => 4 [id_post] => 10 [id_smart_blog_post_shop] => 10 [id_smart_blog_post] => 10 [id_shop] => 1 [id_lang] => 1 [name] => XPPower)
[6] => Array ([id_tag] => 2 [id_post] => 11 [id_smart_blog_post_shop] => 11 [id_smart_blog_post] => 11 [id_shop] => 1 [id_lang] => 1 [name] => Microchip)
[7] => Array ([id_tag] => 2 [id_post] => 12 [id_smart_blog_post_shop] => 12 [id_smart_blog_post] => 12 [id_shop] => 1 [id_lang] => 1 [name] => Microchip)
[8] => Array ([id_tag] => 4 [id_post] => 13 [id_smart_blog_post_shop] => 13 [id_smart_blog_post] => 13 [id_shop] => 1 [id_lang] => 1 [name] => XPPower)
)
和,我要的是显示名称值只有一次这样的:
Microchip, XPPower
而不是这样:
Microchip, Microchip, Microchip, Microchip, Microchip, XPPower, Microchip, Microchip, XPPower
我试图使用array_unique
只显示非重复值,但它部分工作,只显示。
Microchip
值一次和叶XPPower
:
$tags = Db::getInstance()->ExecuteS($sql);
$result = array_values(array_unique($tags));
$this->smarty->assign(array(
'result' => $result
));
阵列打印:
Array (
[0] => Array ([id_tag] => 2 [id_post] => 5 [id_smart_blog_post_shop] => 5 [id_smart_blog_post] => 5 [id_shop] => 1 [id_lang] => 1 [name] => Microchip)
)
这是$ SQL函数,如果有人想知道
$sql = 'SELECT * FROM '._DB_PREFIX_.'smart_blog_post_tag p INNER JOIN
'._DB_PREFIX_.'smart_blog_post_shop s ON p.id_post=s.id_smart_blog_post AND s.id_shop = '.(int) Context::getContext()->shop->id.' INNER JOIN
'._DB_PREFIX_.'smart_blog_tag t ON p.id_tag= t.id_tag where t.id_lang = '.(int)$id_lang.' LIMIT '.$limit;
*我想是为了显示名称值仅一次* - 与那其他列? – splash58
只有名字值对我来说很重要。其他值不会显示在模块前端 – PipBoy2000
如果你不需要其他值,你可以在你的sql查询中尝试'''GROUP by name'''? – Ananth