LoginSignup
4
6

More than 5 years have passed since last update.

重複した要素のみを取得する

Last updated at Posted at 2013-03-21
<?php
$array = ['A', 'A', 'A', 'B', 'B', 'C', 'D'];

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

    return array_keys($duplicate);
}

selectDuplicates($array); // => ["A", "B"]
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6