Php数组获取重复

时间:2015-10-25 16:23:03

标签: php arrays

我有一个Php数组,其值为数组值,时间戳为关键数组,如下所示:

 array(
      144454884=>"12:00am", 145454884=>"12:30am", 144474884=>"1:00am", 144454864=>"1:30am", 143354884=>"1:00am", 144654884=>"1:30am", 1444567584=>"2:00am "
    );

以上示例中的时间戳值不是真实的我写了一个例子,除非你的时区与我的匹配,否则它们无用。

问题: 我需要两次“凌晨1点”和“凌晨1点30分”才能得到重复值1次,如答案所示:

php return only duplicated entries from an array

我需要两次重复值,两个键和值都重复,因为我需要从我的系统上的一周时间中消除那些时间戳,因为夏令时重复,我不想在凌晨1点显示我只想把这个时间显示为不可用。

3 个答案:

答案 0 :(得分:1)

此代码有效:

<?php
$array_new = [];
$array_tmp = [];
$array = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'hello', 5=>'U');

//loop trough all elements in array and for ever element create key
//For "world" key is "world"
//For "World" key is "world"
//For "WORLD" key is "world"
//So all this cases have same key and differenet representation eg. "world" => ["world","World","WORLD"]
foreach($array as $k => $v){
    $index = strtolower($v);
    $array_tmp[$index][] = $v;
}

//loop trough new array with new keys and if there are more than one element(> 1) for some key, all of his representations put in new array
foreach($array_tmp as $k => $v){
    if(count($v) > 1){
        foreach($v as $k2 => $v2){
            $array_new[] = $v2;
        }
    }
}

echo '<pre>';
print_r($array_new);
echo '<pre>';

答案 1 :(得分:1)

我不是100%确定你想要什么,但这是我认为你需要的。 假设您的输入数组被称为$a

$b = array_flip(array_flip($a));
$c = array_diff_key($a, $b);

$b将包含一系列唯一值。 $c将包含已删除的元素。

$b$c的结果如下:

array(5) {
    [144454884] = string(7) "12:00am"
    [145454884] = string(7) "12:30am"
    [143354884] = string(6) "1:00am"
    [144654884] = string(6) "1:30am"
    [1444567584] = string(7) "2:00am "
}

array(2) {
    [144474884] = string(6) "1:00am"
    [144454864] = string(6) "1:30am"
}

答案 2 :(得分:0)

保留关键信息的可能解决方案(我会将中间结果分配给自己的变量,否则可能会让人感到困惑)

$array =  array(
    143354883 => "1:00am",
    144454884 => "12:00am",
    145454884 => "12:30am",
    144474884 => "1:00am",
    144454864 => "1:30am",
    143354884 => "1:00am",
    144654884 => "1:30am",
    1444567584 => "2:00am ",
    0 => 4,
    1 => 4,
    2 => 4,
    3 => "Test",
    4 => "TEST",
    5 => "test "
);

// Used this array_iunique function: http://stackoverflow.com/questions/2276349/case-insensitive-array-unique
function array_iunique($array) {
    return array_intersect_key(
        $array,
        array_unique(array_map("StrToLower",$array))
    );
}

$unique = array_iunique($array);

// Then get the difference by key, that will give you all the duplicate values:
$diff_key = array_diff_key($array, $unique);

// Now we have to find the values that are in the $diff_key and the $unique because we also want to have those:
$correspondingValues = array_uintersect($unique, $diff_key, "strcasecmp");

// Then we have to combine the $duplicate values with the $diff_key and preserve the keys:
$result = array_replace($correspondingValues, $diff_key);
var_dump($result);

将导致:

array(10) {
  [143354883]=>
  string(6) "1:00am"
  [144454864]=>
  string(6) "1:30am"
  [0]=>
  int(4)
  [3]=>
  string(4) "Test"
  [144474884]=>
  string(6) "1:00am"
  [143354884]=>
  string(6) "1:00am"
  [144654884]=>
  string(6) "1:30am"
  [1]=>
  int(4)
  [2]=>
  int(4)
  [4]=>
  string(4) "TEST"
}