如果它是重复的,我需要能够从前一个数组中删除任何一个数组。
$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
答案 0 :(得分: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
答案 1 :(得分: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