如果数组键值重复

时间:2017-11-23 11:22:41

标签: php yii

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

$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)";
}

2 个答案:

答案 0 :(得分:3)

怎么样

package com.tunnelvisionlabs.postgresql;

答案 1 :(得分:2)

使用函数in_array

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

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