当密钥始终唯一时,如何查找重复的数组值?

时间:2015-02-25 05:10:27

标签: php arrays

Array
(
    [02-i-said-i-m-naruto.mp3] => 99
    [018  APNE - TERE SANG.mp3] => 110
    [04-sapana.mp3] => 133
    [04 Kurnu Kuraunu.MP3] => 220
    [005LAD~1.MP3] => 221
    [04-akon-troublemaker_ft._sweet_rush - Copy.mp3] => 237
    [04--somebodys_me.mp3] => 240
    [01-school_of_rock_-_school_of_rock-xxl.mp3] => 253
    [007. The Black Eyed Peas - Imma Be.mp3] => 257
    [04 Timi Hau.mp3] => 266
    [04-jennifer_lopez-(what_is)_love_(produced_by_dmile).mp3] => 267
    [04-LIL~1.mp3] => 275
    [034.Simple Plan - Perfect.mp3] => 278
    [04 THE REAL SLIM SHADY.MP3] => 287
    [007 thoda sa pyar hua hai(01).mp3] => 293
    [02-pearl_jam-even_flow.mp3] => 294
    [04-TUM~yuvraj.mp3] => 337
    [01 - MTV Unplugged - Arjit Singh - Dua [DJMaza].mp3] => 339
    [015  METRO - Alvida.mp3] => 341
    [03 - Sabbra Cadabra.mp3] => 380
    [04 - Sabbra Cadabra.mp3] => 380
    [03 - The Unforgiven II.mp3] => 396
    [04 - The Unforgiven II.mp3] => 396
)

这是我当前的php处理器生成的数组(使用asort()以升序生成值)。现在我需要按歌曲持续时间找到重复的歌曲。例如,03 - Sabbra Cadabra.mp3在那里是重复的。重复的持续时间为380 seconds。我希望在数组值中计算380的数量,或者在值上加粗重复的键。我尝试使用array_count_values($arr),但这没有用。

 Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in index.php on line 23

3 个答案:

答案 0 :(得分:0)

尝试在foreach循环中解析数组值。

$last= '';
$match_array = array();

foreach ( $array_name as $key => $value )
{
  if($value == $last) {
     $match_array[] = $value;
}
$last= $value;
{

$ match_array应该为您提供重复的条目。

答案 1 :(得分:0)

如果需要,消除两个第一行并将数组var添加为$ array。

$dump = "array ( '02-i-said-i-m-naruto.mp3' => '99', '018 APNE - TERE SANG.mp3' => '110', '04-sapana.mp3' => '133', '04 Kurnu Kuraunu.MP3' => '220', '005LAD~1.MP3' => '221', '04-akon-troublemaker_ft._sweet_rush - Copy.mp3' => '237', '04--somebodys_me.mp3' => '240', '01-school_of_rock_-_school_of_rock-xxl.mp3' => '253', '007. The Black Eyed Peas - Imma Be.mp3' => '257', '04 Timi Hau.mp3' => '266', '04-jennifer_lopez-(what_is)_love_(produced_by_dmile).mp3' => '267', '04-LIL~1.mp3' => '275', '034.Simple Plan - Perfect.mp3' => '278', '04 THE REAL SLIM SHADY.MP3' => '287', '007 thoda sa pyar hua hai(01).mp3' => '293', '02-pearl_jam-even_flow.mp3' => '294', '04-TUM~yuvraj.mp3' => '337', '01 - MTV Unplugged - Arjit Singh - Dua [DJMaza].mp3' => '339', '015 METRO - Alvida.mp3' => '341', '03 - Sabbra Cadabra.mp3' => '380', '04 - Sabbra Cadabra.mp3' => '380', '03 - The Unforgiven II.mp3' => '396', '04 - The Unforgiven II.mp3' => '396', )";

eval("\$array=$dump;");

$array1 = array_count_values($array);

print_r(array_filter($array1, "duplicate"));

function duplicate($var) {
  return $var > 1;
}

答案 2 :(得分:0)

我认为array_uniquearray_diff_assoc是更好的选择。 以下代码将为您提供具有重复时间值的数组:

$array = array
(
    '02-i-said-i-m-naruto.mp3' => 99,
    '018  APNE - TERE SANG.mp3' => 110,
    '04-sapana.mp3' => 133,
    '04 Kurnu Kuraunu.MP3' => 220,
    '005LAD~1.MP3' => 221,
    '04-akon-troublemaker_ft._sweet_rush - Copy.mp3' => 237,
    '04--somebodys_me.mp3' => 240,
    '01-school_of_rock_-_school_of_rock-xxl.mp3' => 253,
    '007. The Black Eyed Peas - Imma Be.mp3' => 257,
    '04 Timi Hau.mp3' => 266,
    '04-jennifer_lopez-(what_is)_love_(produced_by_dmile).mp3' => 267,
    '04-LIL~1.mp3' => 275,
    '034.Simple Plan - Perfect.mp3' => 278,
    '04 THE REAL SLIM SHADY.MP3' => 287,
    '007 thoda sa pyar hua hai(01).mp3' => 293,
    '02-pearl_jam-even_flow.mp3' => 294,
    '04-TUM~yuvraj.mp3' => 337,
    '01 - MTV Unplugged - Arjit Singh - Dua [DJMaza].mp3' => 339,
    '015  METRO - Alvida.mp3' => 341,
    '03 - Sabbra Cadabra.mp3' => 380,
    '04 - Sabbra Cadabra.mp3' => 380,
    '03 - The Unforgiven II.mp3' => 396,
    '04 - The Unforgiven II.mp3' => 396,
);

$arrayUnique = array_unique($array);
print_r(array_diff_assoc($array, $arrayUnique));

会给:

Array
(
    [04 - Sabbra Cadabra.mp3] => 380
    [04 - The Unforgiven II.mp3] => 396
)