简体   繁体   中英

Php array get all values that are repeating

I have a Php array that have values of times as array values and timestamps as key array is like this:

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

Timestamp values in above example are not real I wrote an example they are useless anyway unless your timezone matches mine.

Problem: I need to get "1:00am" and "1:30am" twice I can get repeating values 1 time as shown in answer here:

php return only duplicated entries from an array

I need both repeating values two times with both keys and values being repeated because I need to eliminate those timestamps from week time on my system because of daylight saving a time is repeating and I don't want to show 1:00am at all I just want to show this time as unavailable.

This code works :

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

I am not 100% sure what you wanted but this is what I think you need. Assuming your input array is called $a

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

$b will contain an array of unique values. $c will contain the elements that were removed.

Results of $b and $c are as follows:

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

A possible solution keeping the key information (I will assign the intermediate results to their own variables otherwise it can be confusing to read)

$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);

Will result in:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM