0

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.

5
  • 1
    You can't have duplicate keys in an array.... array keys must be unique.... Quoting from the 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. Commented Oct 25, 2015 at 16:23
  • I think if your source use utc time and then use php datetime class to convert back to your time then you dont need to deal with daylight / leaf year Commented Oct 25, 2015 at 16:26
  • I have a complex function with UTC arrays of times of the day Andrew in my server time this problem occurs while displaying for user's timezone. Commented Oct 25, 2015 at 16:37
  • Mark yes I don't have duplicate keys I have duplicate values timestamps are different for those two times but since with daylight savings clock is rewind for United Kingdom at 25th October 2015 at 1:00am so 1:00am and 1:30am occurs twice please check here, timeanddate.com/time/change/uk/london?year=2015 Commented Oct 25, 2015 at 16:39
  • I'm aware of the time change.... I just got an extra hours sleep as a result of it. I need both repeating values two times with both keys and values being repeated sounds as though you want multiple values with the same key (being repeated) to me Commented Oct 25, 2015 at 21:41

3 Answers 3

1

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"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes thanks array_flip() twice should do the trick it should give me unique values removing duplicate 1:00am and also keeping keys. This works.
1

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

3 Comments

Yes code works could you please explain it a bit with some comments and could you use my array as example please I need to understand and use this. Need to know why this is working.
Thanks I was able to solve this problem with this code, this code loses key information though which I wanted to preserve keys but it helped.
You can save "key information" and "representation" if you extend this code snippet a little bit.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.