Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:获取字符串的并集_Php_String - Fatal编程技术网

PHP:获取字符串的并集

PHP:获取字符串的并集,php,string,Php,String,可能重复: 有一个字符串,如: $str = "1 - 1 - 2 - 3 - 1 - 4 - 3 - 1 - 2 - 5 - 6 - 4"; 我需要得到 $str = "1 - 2 - 3 - 4 - 5 - 6"; 有什么帮助吗(示例/: 大概是这样的: <?php $str = str_replace(' - ', ',', $str); $items = array_unique(explode(',', $str)); echo implode(' - ', $items)

可能重复:

有一个字符串,如:

$str = "1 - 1 - 2 - 3 - 1 - 4 - 3 - 1 - 2 - 5 - 6 - 4";
我需要得到

$str = "1 - 2 - 3 - 4 - 5 - 6";
有什么帮助吗(

示例/:

大概是这样的:

<?php
$str = str_replace(' - ', ',', $str);
$items = array_unique(explode(',', $str));
echo implode(' - ', $items);
?>
$strArray = explode(' - ',$str);
str = array_unique($strArray);


我已交换了空格hifen空格分隔符,这样您就可以看到中间步骤中发生了什么-但如果您愿意,可以立即进行分解。

如果必须使用唯一值创建新字符串,您可以尝试以下操作:

implode(' - ', array_unique(explode(' - ', $array)));

我刚刚快速浏览了一下,并使用了

这对你有用


你可以看到。

是的,我会这样做:

<?php
$str = str_replace(' - ', ',', $str);
$items = array_unique(explode(',', $str));
echo implode(' - ', $items);
?>
$strArray = explode(' - ',$str);
str = array_unique($strArray);

或者类似的东西。

errr,你可能想把它重新分解为$str:PAh排序,好的地方。我投了反对票,因为最好是用一个代码示例来解释你所做的尝试。坐下来看一本手册,先试一下是学习
:)的最好方法。