合并两个数字字符串


Merging two strings of numbers

我有两个字符串,都由数字组成。

$str1 = '2,3,6,8,99';
$str2 = '44,22,4,3,6';
我想以一种从

最小到最大对它们进行排序并且不会有重复值的方式合并它们。所以我认为最好的方法是使用数组函数;

$str1 = explode(', ', $str1 );
$str2 = explode(', ', $str2 );
$merged= array_merge($str1, $str2);
sort($merged); // sort low to high
$str3 = array_unique($merged); // remove duplicates
$str3 = implode(', ', $str3 );

看起来差不多,但我仍然得到重复项并且列表没有排序......我错过了什么?

你在错误的分隔符上爆炸。请注意,的额外空间。

我很

确定这应该有效,我会在几分钟内尝试。

$tempStr1 = explode(',',$str1);
$tempStr2 = explode(',',$str2);
$tempArr = array_merge($tempStr1, $tempStr2);
$tempArr = array_unique($tempArr);
echo implode(',', $tempArr);

[编辑]
看起来你解决了它..