首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Php数组获取所有重复的值

问Php数组获取所有重复的值
EN

Stack Overflow用户
提问于 2015-10-25 16:23:02
回答 3查看 1.4K关注 0票数 0

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

代码语言:javascript
复制
 array(
      144454884=>"12:00am", 145454884=>"12:30am", 144474884=>"1:00am", 144454864=>"1:30am", 143354884=>"1:00am", 144654884=>"1:30am", 1444567584=>"2:00am "
    );

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

问题:我需要得到“凌晨1:00”和“凌晨1:30”两次,我可以得到1次重复值,如下所示:

php return only duplicated entries from an array

我需要两次重复值,同时重复键和值,因为我需要消除系统上每周时间的时间戳,因为夏时制是重复的,我根本不想显示凌晨1:00,我只想显示这个时间不可用。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-26 17:10:12

我不是百分之百确定你想要什么,但这是我认为你需要的。假设输入数组名为$a

代码语言:javascript
复制
$b = array_flip(array_flip($a));
$c = array_diff_key($a, $b);

$b将包含一个唯一值数组。$c将包含已删除的元素。

$b和$c的结果如下:

代码语言:javascript
复制
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"
}
票数 1
EN

Stack Overflow用户

发布于 2015-10-25 16:31:40

此代码适用于:

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2015-10-25 19:55:42

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

代码语言:javascript
复制
$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);

将导致:

代码语言:javascript
复制
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"
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33331996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档