2013-08-21 67 views
0

我需要能够从前一个阵列中删除任何阵列,如果它是重复的。删除重复一个阵列的阵列

$toarray = cccccce,cccccce,f5f5f5,f5f5f5,e8e8e8,cccccce,8e8e8c,ccccce,8e8e8f,fffffd 

$color = $toarray; 
$color = explode(",",$color); 
$color = array_unique($color); 
$color = implode(",", $color); 

所以我需要它看起来像这样。

cccccce,f5f5f5,e8e8e8,cccccce,8e8e8c,ccccce,8e8e8f,fffffd 
+1

'$ toarray'不是一个字符串,除此之外,什么是问题,任何错误? – elclanrs

+0

它删除所有具有相同数组上下文的相同数组。我只需要删除它旁边的数组。 – Brian

回答

1

您将需要一个经过阵列的内容之一,并与它旁边的一个比较值。

$toarray = "cccccce,cccccce,f5f5f5,f5f5f5,e8e8e8,cccccce,8e8e8c,ccccce,8e8e8f,fffffd"; 
$array = explode (',', $toarray); // Convert the string to array 

for ($i = 0, $len = count ($array); $i < $len - 1; $i++) 
{ 
    // Compare against the value next to current item 
    if ($array[$i] == $array[$i + 1]) 
    { 
     unset ($array[$i]); 
    } 
} 

print implode (', ', $array); 
// Result: cccccce, f5f5f5, e8e8e8, cccccce, 8e8e8c, ccccce, 8e8e8f, fffffd 
+1

在发布之前,我实际上已经用这种方法计算出来了。但欢呼声,非常感谢。 – Brian

1
$toarray = array("cccccce","cccccce","f5f5f5","f5f5f5","e8e8e8","cccccce","8e8e8c","ccccce","8e8e8f","fffffd"); 

$color = array_unique($toarray); 

$result = implode(",", $color); 
print_r($result); 

结果

cccccce,f5f5f5,e8e8e8,8e8e8c,ccccce,8e8e8f,fffffd