仅回显array_count_values结果php的重复值

时间:2017-06-22 10:19:05

标签: php arrays sorting duplicates

在我和朋友搜索了几个小时后,尝试了很多事情后,我们无法找到如何只回显array_count_values结果的重复值。我会把它分解:

我们有许多选择框,通过GET发送时构建数组,例如:

    <div class="form-group col-sm-2 mb-sm">
            <select name="select[]" class="form-control">
                <option value="" disabled="" selected="">Select</option>
                <option value="optionOne">Option 1</option>
                <option value="optionTwo">Option 2</option>
            </select>
        </div>
        <div class="form-group col-sm-2 mb-sm">
            <select name="select[]" class="form-control">
                <option value="" disabled="" selected="">Select</option>
                <option value="optionOne">Option 1</option>
                <option value="optionTwo">Option 2</option>
            </select>
        </div>
        <div class="form-group col-sm-2 mb-sm">
            <select name="select[]" class="form-control">
                <option value="" disabled="" selected="">Select</option>
                <option value="optionOne">Option 1</option>
                <option value="optionTwo">Option 2</option>
            </select>
        </div>

我们正在做以下事情:

if (max(array_count_values($_GET['select'])) == 2) {
   $twoSelected = '2 of the selections are the same, which were (DUPLICATE SELECTION HERE)';
}

我们尝试了一个foreach循环,但似乎无法让它工作。

非常感谢任何帮助。

亲切的问候

2 个答案:

答案 0 :(得分:1)

试试这个

$array = array("test", "hello", "test", "world", "hello");

$duplicatedValuesArray = array_keys(array_filter(array_count_values($array), function($v) {
    return $v > 1;
}));

echo implode(', ',$duplicatedValuesArray);

答案 1 :(得分:0)

我认为以下代码有助于您获得重复值。

$array = array(1=>'12334',2=>'123345',3 =>'Helloo' ,4=>'hello', 5=>'helloo');

// Convert every array value to uppercase, and remove all duplicate values
$notdublicates = array_unique(array_map("strtoupper", $array));

// The difference in the original array $array, and the $notdublicates array
// will be the duplicate values
$getduplicates = array_diff($array, $notdublicates);
print_r($getduplicates);