如果数组键值重复

if array key value is duplicate

我有以下数组值,这里重复花费,

$array = array('Spend','Spend','Form Conversions','Phone Conversions')

我可以像这样检测到多个条目

print_r(array_count_values($array));

输出是

Array
(
   [Spend] => 2
   [Form Conversions] => 1
   [Phone Conversions] => 1
)

如何将单独的条件打印如下

if(any key(here spend) found with count = 2 )
{
echo $duplicate element; //edits
}
else
{
 echo $no_duplicate_elements //edits
echo "count = 1 (here Form Conversions,Phone Conversions)";
}

怎么样

foreach(array_count_values($array) as $value => $count){
    if($count > 1){
        echo "hi " . $value . "*";
    }else{
       echo "count = 1 " . $value . "*";
    }
}

?

使用函数in_array

if (in_array(2, array_count_values($array))) {
    echo "there is at least one word with count = 2";
}

如果您想知道哪些元素是重复的,那么您必须使用 foreach - 查看 Jameson the dog 的回答。