获取数组中重复值的键

时间:2011-06-23 21:48:26

标签: php arrays duplicates key

我有以下数组:

$myarray = Array("2011-06-21", "2011-06-22", "2011-06-22", "2011-06-23", "2011-06-23", "2011-06-24", "2011-06-24", "2011-06-25", "2011-06-25", "2011-06-26");
var_dump($myarray);

结果:

Array (
    [0] => 2011-06-21
    [1] => 2011-06-22
    [2] => 2011-06-22
    [3] => 2011-06-23
    [4] => 2011-06-23
    [5] => 2011-06-24
    [6] => 2011-06-24
    [7] => 2011-06-25
    [8] => 2011-06-25
    [9] => 2011-06-26
)
  1. 现在如何显示具有重复值的键?这里函数不应该返回([0],[9]),因为没有重复的值。
  2. 如何找到相同值的键,例如对于“2011-06-25”它应该返回[7],[8]

10 个答案:

答案 0 :(得分:20)

function get_keys_for_duplicate_values($my_arr, $clean = false) {
    if ($clean) {
        return array_unique($my_arr);
    }

    $dups = $new_arr = array();
    foreach ($my_arr as $key => $val) {
      if (!isset($new_arr[$val])) {
         $new_arr[$val] = $key;
      } else {
        if (isset($dups[$val])) {
           $dups[$val][] = $key;
        } else {
           $dups[$val] = array($key);
           // Comment out the previous line, and uncomment the following line to
           // include the initial key in the dups array.
           // $dups[$val] = array($new_arr[$val], $key);
        }
      }
    }
    return $dups;
}

显然函数名有点长;)

现在$ dups将包含一个由重复值键入的多维数组,其中包含每个重复的键,如果发送“true”作为第二个参数,它将返回没有重复值的原始数组。

或者,您可以将原始数组作为参考传递,并在返回重复数组时相应地调整它

答案 1 :(得分:13)

我先回答第二个问题。您希望将array_keys与指定的“search_value”一起使用。

$keys = array_keys($array, "2011-06-29")

在下面的示例中,$duplicates将包含重复值,而$result将包含不重复的值。要获取密钥,只需使用array_keys

<?php

$array = array(
  'a',
  'a',
  'b',
  'c',
  'd'
);

// Unique values
$unique = array_unique($array);

// Duplicates
$duplicates = array_diff_assoc($array, $unique);

// Unique values
$result = array_diff($unique, $duplicates);

// Get the unique keys
$unique_keys = array_keys($result);

// Get duplicate keys
$duplicate_keys = array_keys(array_intersect($array, $duplicates));

结果:

// $duplicates
Array
(
    [1] => a
)

// $result
Array
(
    [2] => b
    [3] => c
    [4] => d
)

// $unique_keys
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
)

// $duplicate_keys
Array
(
    [0] => 0
    [1] => 1
)

答案 2 :(得分:1)

$array = array(0 => "1", 1 => "1", 2 => "2", 3 => "3");
$count = array();
foreach($array as $key => $value) {
  if(!isset($count[$value])) {
    $count[$value] = 0;
  }
  $count[$value]++;
}


$result = array_filter($count, function($value) {
  return $value > 1;
});

$result = array_keys($result);

var_dump($result);

输出

array(1) {
  [0]=>
  int(1)
}

答案 3 :(得分:1)

这是一个代码dude

   $your_array = array(0 => '2011-06-21', 1 => '2011-06-22', 2 => '2011-06-22', 3 => '2011-06-23', 4 =>
'2011-06-23', 5 => '2011-06-24', 6 => '2011-06-24', 7 => '2011-06-25', 8 => '2011-06-25', 9 
=> '2011-06-26', 10 => '2011-06-26', 11 => '2011-06-27', 12 => '2011-06-27', 13 => '2011-06-  
28', 14 => '2011-06-29', 15 => '2011-06-29', 16 => '2011-06-30', 17 => '2011-06-30', 18 => 
'2011-07-01', 19 => '2011-07-01', 20 => '2011-07-02', 21 => '2011-07-02', 22 => '2011-07-03', 
23 => '2011-07-03', 24 => '2011-07-04', 25 => '2011-07-04', 26 => '2011-07-05', 27 => '2011-
07-05', 28 => '2011-07-06', 29 => '2011-07-06', 30 => '2011-07-07', 31 => '2011-07-07');

$keys_of_duplicated = array();
$array_keys = array();

foreach($your_array as $key => $value) {
    //- get the keys of the actual value
    $array_keys = array_keys($your_array, $value);

    //- if there is more then one key collected we register it
    if(count($array_keys) > 1) {
        //- foreach key that have the same value we check if i'ts already registered
        foreach($array_keys as $key_registered) {
            //- if not registered we register it
            if(!in_array($key_registered,  $keys_of_duplicated)) {
                 $keys_of_duplicated[] = $key_registered;
            }
        }
    }
}

var_dump($keys_of_duplicated);

$ keys_of_duplicated现在是包含重复数组键的数组;)再见

答案 4 :(得分:1)

我遇到了与OP问题#1类似的问题。我需要的只是原始数组中重复值的键。这就是我想出的:

$array = array('yellow', 'red', 'green', 'brown', 'red', 'brown');

$counts = array_count_values($array);
$filtered = array_filter($counts, function($value) {
    return $value != 1;
});
$result = array_keys(array_intersect($array, array_keys($filtered)));

输出:

print_r($result);
Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 5
)

答案 5 :(得分:1)

function getDuplicateValueKeys($my_arr, $clean = false) 
{
    if ($clean) {
        return array_unique($my_arr);
    }
    $dups = array();
    $new_arr = array();
    $dup_vals = array();

    foreach ($my_arr as $key => $value) {
        if (!isset($new_arr[$value])) {
            $new_arr[$value] = $key;
        } else {
            array_push($dup_vals,$value);
        }
    }

    foreach ($my_arr as $key => $value) {
        if (in_array($value, $dup_vals)) {
            if (!isset($dups[$value])) {
                $dups[$value]=array($key);
            }else{
                array_push($dups[$value],$key);
            }
        }
    }

    return $dups;
}

答案 6 :(得分:0)

$array = array(
    '2011-06-21','2011-06-22','2011-06-22','2011-06-23',
    '2011-06-23','2011-06-24','2011-06-24','2011-06-25',
    '2011-06-25','2011-06-26','2011-06-26','2011-06-27',
    '2011-06-27','2011-06-28','2011-06-29','2011-06-29',
    '2011-06-30','2011-06-30','2011-07-01','2011-07-01',
    '2011-07-02','2011-07-02','2011-07-03','2011-07-03',
    '2011-07-04','2011-07-04','2011-07-05','2011-07-05',
    '2011-07-06','2011-07-06','2011-07-07','2011-07-07',
);

function getDupKeys(array $array, $return_first = true, $return_by_key = true) {
    $seen = array();
    $dups = array();

    foreach ($array as $k => $v) {
        $vk = $return_by_key ? $v : 0;
        if (!array_key_exists($v, $seen)) {
            $seen[$v] = $k;
            continue;
        }
        if ($return_first && !array_key_exists($v, $dups)) {
            $dups[$vk][] = $seen[$v];
        }
        $dups[$vk][] = $k;
    }
    return $return_by_key ? $dups : $dups[0];
}

如果两个可选参数均为true,则返回一个数组数组;每个子数组的键将是不唯一的值,并且数组的值将是具有该值的所有键。

如果第一个可选参数为false,则只返回之后的非唯一值的第一个实例<(对于给定的数组,每个值只返回一个键,第二次发生,而不是第一次。

如果第二个参数是可选的,那么它不是返回一个数组数组,而是返回一个包含所有重复键的平面数组(它返回的确切键由前面的可选参数决定)。

这是一个 dump print_r,因为它更漂亮:

print_r(getDupKeys($array));

Array
(
    [2011-06-22] => Array
        (
            [0] => 1
            [1] => 2
        )

    [2011-06-23] => Array
        (
            [0] => 3
            [1] => 4
        )

    [2011-06-24] => Array
        (
            [0] => 5
            [1] => 6
        )

    [2011-06-25] => Array
        (
            [0] => 7
            [1] => 8
        )

    [2011-06-26] => Array
        (
            [0] => 9
            [1] => 10
        )

    [2011-06-27] => Array
        (
            [0] => 11
            [1] => 12
        )

    [2011-06-29] => Array
        (
            [0] => 14
            [1] => 15
        )

    [2011-06-30] => Array
        (
            [0] => 16
            [1] => 17
        )

    [2011-07-01] => Array
        (
            [0] => 18
            [1] => 19
        )

    [2011-07-02] => Array
        (
            [0] => 20
            [1] => 21
        )

    [2011-07-03] => Array
        (
            [0] => 22
            [1] => 23
        )

    [2011-07-04] => Array
        (
            [0] => 24
            [1] => 25
        )

    [2011-07-05] => Array
        (
            [0] => 26
            [1] => 27
        )

    [2011-07-06] => Array
        (
            [0] => 28
            [1] => 29
        )

    [2011-07-07] => Array
        (
            [0] => 30
            [1] => 31
        )

)

print_r(getDupKeys($array, false));

Array
(
    [2011-06-22] => Array
        (
            [0] => 2
        )

    [2011-06-23] => Array
        (
            [0] => 4
        )

    [2011-06-24] => Array
        (
            [0] => 6
        )

    [2011-06-25] => Array
        (
            [0] => 8
        )

    [2011-06-26] => Array
        (
            [0] => 10
        )

    [2011-06-27] => Array
        (
            [0] => 12
        )

    [2011-06-29] => Array
        (
            [0] => 15
        )

    [2011-06-30] => Array
        (
            [0] => 17
        )

    [2011-07-01] => Array
        (
            [0] => 19
        )

    [2011-07-02] => Array
        (
            [0] => 21
        )

    [2011-07-03] => Array
        (
            [0] => 23
        )

    [2011-07-04] => Array
        (
            [0] => 25
        )

    [2011-07-05] => Array
        (
            [0] => 27
        )

    [2011-07-06] => Array
        (
            [0] => 29
        )

    [2011-07-07] => Array
        (
            [0] => 31
        )

)

print_r(getDupKeys($array, true, false));

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 14
    [13] => 15
    [14] => 16
    [15] => 17
    [16] => 18
    [17] => 19
    [18] => 20
    [19] => 21
    [20] => 22
    [21] => 23
    [22] => 24
    [23] => 25
    [24] => 26
    [25] => 27
    [26] => 28
    [27] => 29
    [28] => 30
    [29] => 31
)

print_r(getDupKeys($array, false, false));

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
    [5] => 12
    [6] => 15
    [7] => 17
    [8] => 19
    [9] => 21
    [10] => 23
    [11] => 25
    [12] => 27
    [13] => 29
    [14] => 31
)

答案 7 :(得分:0)

我真的很喜欢弗朗索瓦的回答,这是我提出的保留钥匙的东西。我先回答第一个问题:

$array = array('2011-06-21', '2011-06-22', '2011-06-22');
/**
 * flip an array like array_flip but
 * preserving multiple keys per an array value
 * 
 * @param array $a
 * @return array
 */
function array_flip_multiple(array $a) {
    $result = array();
    foreach($a as $k=>$v)
        $result[$v][]=$k
        ;
    return $result;
}

$hash = array_flip_multiple($array);

// filter $hash based on your specs (2 or more)
$hash = array_filter($hash, function($items) {return count($items) > 1;});

// get all remaining keys
$keys = array_reduce($hash, 'array_merge', array());

var_dump($array, $hash, $keys);

输出是:

# original array
array(3) {
  [0]=>
  string(10) "2011-06-21"
  [1]=>
  string(10) "2011-06-22"
  [2]=>
  string(10) "2011-06-22"
}

# hash (filtered)
array(1) {
  ["2011-06-22"]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    int(2)
  }
}

# the keys
array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}

所以现在第二个问题:

只需使用$hash获取值的键:

var_dump($hash['2011-06-22']);返回键。

好处是,如果您需要检查多个值,数据已经存储在哈希中并可供使用。

答案 8 :(得分:0)

另一个例子:

$array = array(
  'a',
  'a',
  'b',
  'b',
  'b'
);
echo '<br/>Array: ';
print_r($array);
// Unique values
$unique = array_unique($array);
echo '<br/>Unique Values: ';
print_r($unique);
// Duplicates
$duplicates = array_diff_assoc($array, $unique);
echo '<br/>Duplicates: ';
print_r($duplicates);
// Get duplicate keys
$duplicate_values = array_values(array_intersect($array, $duplicates));
echo '<br/>duplicate values: ';
print_r($duplicate_values);

输出:

Array :Array ( [0] => a [1] => a [2] => b [3] => b [4] => b ) 
Unique Values :Array ( [0] => a [2] => b ) 
Duplicates :Array ( [1] => a [3] => b [4] => b ) 
Duplicate Values :Array ( [0] => a [1] => a [2] => b [3] => b [4] => b ) 

答案 9 :(得分:0)

这是我用来获取一维数组中的重复项的问题 1 的另一个解决方案:

以这个数组为例:

$arr = ['a','b','c','d','a','a','b','c','c'];

我喜欢尽可能避免循环,所以我使用 array_map 来解决这个问题:

/**
 * Find multiple occurrences in a one-dimensional array
 *
 * @param array $list List to find duplicates for.
 *
 * @return array
 */
function fetchDuplicates(array $list)
{
    return array_filter(
        array_map(
            function ($el) use ($list) {
                $keysOccur = array_keys($list, $el);
                if (count($keysOccur) > 1) {
                    return $keysOccur;
                }

                return null;
            },
            array_unique($list)
        )
    );

}//end fetchDuplicates()

array_map 函数循环 $list 的每个唯一元素,并使用原始 $list 获取 array_keys($list, $el) 出现的数组键。如果结果出现次数大于 1,则返回所有出现次数的索引,并将原始 $el 替换为索引数组。否则返回 null,它也会替换 $el 的唯一变体中的 $list。最后,结果数组被过滤为空值(与 null 值的情况一样)。产生以下数组:

[
  [
    0,
    4,
    5,
  ],
  [
    1,
    6,
  ],
  [
    2,
    7,
    8,
  ],
]