我有一个逗号分隔数字的数组,我想删除所有重复的数字。这种方法是最好的选择还是更智能?
$filter_tags_array[] = '4,6,2,5,8';
$filter_tags_array[] = '6,8,1,3,5,7,2,4';
$filter_tags_array[] = '2,4,1,3,5';
$filter_tags_array[] = '6,3,5,8,2,4';
$filter_tags_array[] = '2,8,11,4,9,12';
$filter_tags_array[] = '9,11,2,8,10,12,4';
$tags_id = '';
foreach($filter_tags_array as $filter_tags) {
$tags_id .= $filter_tags . ',';
}
$tags_id = implode(',', array_unique(explode(',', $tags_id)));
echo "<pre>";
print_r($tags_id);
print_r结果:4,6,2,5,8,1,3,7,11,9,12,10,
答案 0 :(得分:2)
最好是内插数组而不是连接每个字符串。这是因为php每次都必须创建一个新的字符串。
答案 1 :(得分:1)
这应该对你有用,请看这里
$filter_tags_array[] = '4,6,2,5,8';
$filter_tags_array[] = '6,8,1,3,5,7,2,4';
$filter_tags_array[] = '2,4,1,3,5';
$filter_tags_array[] = '6,3,5,8,2,4';
$filter_tags_array[] = '2,8,11,4,9,12';
$filter_tags_array[] = '9,11,2,8,10,12,4';
$tags_id = implode(',',array_unique(explode(',', (implode(',', $filter_tags_array)))));
print($tags_id);