【问题标题】:Php array get all values that are repeatingphp数组获取所有重复的值
【发布时间】:2015-10-25 16:23:02
【问题描述】:

我有一个 PHP 数组,它的时间值作为数组值,时间戳作为键数组,如下所示:

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

上面示例中的时间戳值不是真实的,我写了一个示例,除非您的时区与我的时区匹配,否则它们无论如何都没用。

问题: 我需要两次获得“1:00am”和“1:30am”我可以获得重复值1次,如此处的答案所示:

php return only duplicated entries from an array

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

【问题讨论】:

  • 数组中不能有重复的键......数组键必须是唯一的......引用PHP Docs:If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
  • 我认为如果您的来源使用 utc 时间,然后使用 php datetime 类转换回您的时间,那么您不需要处理日光/叶年
  • 我有一个复杂的函数,其中包含一天中 Andrew 在我的服务器时间的 UTC 时间数组,在显示用户的时区时会出现此问题。
  • 标记是的,我没有重复的键,我有重复的值,这两次的时间戳不同,但由于夏令时时钟在 2015 年 10 月 25 日凌晨 1:00 为英国倒带,所以 1: 00am 和 1:30am 出现两次,请在此处查看,timeanddate.com/time/change/uk/london?year=2015
  • 我知道时间变化了……因此我多睡了几个小时。 I need both repeating values two times with both keys and values being repeated 听起来好像你想要多个具有相同键的值 (being repeated) 对我来说

标签: php arrays


【解决方案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"
}

【讨论】:

  • 是的,谢谢 array_flip() 两次应该可以做到这一点,它应该给我唯一的值,删除重复的 1:00am 并保留密钥。这行得通。
【解决方案2】:

此代码有效:

<?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>';

【讨论】:

  • 是的代码有效,请您用一些 cmets 稍微解释一下,您能否以我的数组为例,我需要理解并使用它。需要知道为什么这是有效的。
  • 谢谢我用这段代码解决了这个问题,这段代码丢失了密钥信息,虽然我想保留密钥但它有所帮助。
  • 把这段代码sn-p稍微扩展一下就可以保存“关键信息”和“表示”了。
【解决方案3】:

保留关键信息的可能解决方案(我会将中间结果分配给它们自己的变量,否则可能会造成阅读混乱)

$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"
}

【讨论】:

    猜你喜欢
    • 2012-05-03
    • 2022-01-15
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    相关资源
    最近更新 更多