Php 数组差异仅使用数组中的第一个字

Php 数组差异仅使用数组中的第一个字,php,arrays,echo,explode,implode,Php,Arrays,Echo,Explode,Implode,使用array_diff过滤我的数组,并确保如果单词在过滤器中,它将不会包含在回音中。现在,如果我用“test1,test4”替换array2,输出将是“test2,test3”,这是正确的,但是如果我用“test2,test4”替换array2,它将输出“test1,test3”,我将得到“test1,test2,test3”的输出,因此它不会过滤掉它。我知道这可能是一个非常简单的修复,我只是忽略了它。我将在下面发布代码 <?php $array1 = "test1, test2, te

使用array_diff过滤我的数组,并确保如果单词在过滤器中,它将不会包含在回音中。现在,如果我用“test1,test4”替换array2,输出将是“test2,test3”,这是正确的,但是如果我用“test2,test4”替换array2,它将输出“test1,test3”,我将得到“test1,test2,test3”的输出,因此它不会过滤掉它。我知道这可能是一个非常简单的修复,我只是忽略了它。我将在下面发布代码

<?php
$array1 = "test1, test2, test3";
$array2 = "test2, test4";

$myArray = explode(',', $array1);
$myArray2 = explode(',', $array2);
$unique=array_diff($myArray, $myArray2);
echo implode(',', $unique); 
?>

您需要将
修剪
数组映射
一起使用,如下代码所示

$array1 = "test1, test2, test3";
$array2 = "test2, test4";

$myArray = array_map('trim', explode(',', $array1));
$myArray2 = array_map('trim', explode(',', $array2));

$unique=array_diff($myArray, $myArray2);
echo implode(',', $unique); 

您需要将
trim
array\u map
一起使用,如下代码所示

$array1 = "test1, test2, test3";
$array2 = "test2, test4";

$myArray = array_map('trim', explode(',', $array1));
$myArray2 = array_map('trim', explode(',', $array2));

$unique=array_diff($myArray, $myArray2);
echo implode(',', $unique); 

使用带有空格的
分解

$myArray = explode(', ', $array1);
$myArray2 = explode(', ', $array2);

使用带有空格的
分解

$myArray = explode(', ', $array1);
$myArray2 = explode(', ', $array2);

您需要在逗号空间上分解(),因此,
,“
或除第一个字符外的所有字符都有一个空格作为第一个字符。您需要在逗号空间上分解(),因此,
,“
或除第一个字符外的所有字符都有一个空格作为第一个字符。为什么不在分解()中使用空格?是的,您是对的@AbraCadaver先生也可以使用,但也可能是在值之前和/或之后,为什么不在explode()中使用空格?是的,您是对的@AbraCadaver先生也可以使用,但也可能是在值之前和/或之后