4

Using the example below, how can I remove all duplicate top level array elements that meet the following criteria:

  • same TicketID_xxxxx and Ticket_Reply_xxxxx numbers (where xxxxx is the number)
  • also have matching timestamps?

Edits for additional info:

  • I will always need to remove the sub-array with TicketID_xxxxx while keeping the sub-array with Ticket_Reply_xxxxx

Starting array:

Array
(
    [0] => Array
        (
            [0] => 2018-03-03 07:43:15
            [1] => TicketID_25500
        )

     [1] => Array
         (
            [0] => 2018-03-03 08:00:00 //matching timestamp
            [1] => TicketID_25500
        )

    [2] => Array
        (
            [0] => 2018-03-03 08:00:00 //matching timestamp
            [1] => Ticket_Reply_25500
        )
}

Desired Result:

Array
(
    [0] => Array
        (
            [0] => 2018-03-03 07:43:15
            [1] => TicketID_25500
        )

    [1] => Array
        (
            [0] => 2018-03-03 08:00:00
            [1] => Ticket_Reply_25500
        )
}
lin web
  • 45
  • 4
  • 1
    You can try `array_unique();` – Ayaz Ali Shah Mar 05 '18 at 10:20
  • Not sure if `array_unique()` would work for this array as your output will be the same as the input (for the above example). Fails on the timestamp for array[0] and array[1] because timestamps are different – IsThisJavascript Mar 05 '18 at 10:22
  • In your example: How do you know which one you need to keep between `[1]` and `[2]` from the "starting array"? – Clément Baconnier Mar 05 '18 at 10:38
  • @Mr.Developer I tried some of the methods in https://stackoverflow.com/questions/13857775/remove-duplicated-elements-of-associative-array-in-php , but that topic is focused on all sub values matching. Could you give an example how I can use array_unique() in this case? – lin web Mar 05 '18 at 10:56
  • @cbaconnier I will always need to remove the TicketID_xxxxx while keeping the Ticket_Reply_xxxxx – lin web Mar 05 '18 at 10:57

3 Answers3

2

You can use SORT_REGULAR option More doc is HERE about array_uniqy()

    <?php

$result = array(
    0=>array(0=>'2018-03-03 07:43:15',1=>'TicketID_25500'),
    1=>array(0=>'2018-03-03 08:00:00',1=>'TicketID_25500'),
    2=>array(0=>'2018-03-03 08:00:00',1=>'Ticket_Reply_25500'),
);

$details = unique_multidim_array($result ,'1'); 
print_r($details);
function unique_multidim_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 

    foreach($array as $val) { 
        if (!in_array($val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
        } 
        $i++; 
    } 
    return $temp_array; 
} 

O/P is:

Array
(
    [0] => Array
        (
            [0] => 2018-03-03 07:43:15
            [1] => TicketID_25500
        )

    [2] => Array
        (
            [0] => 2018-03-03 08:00:00
            [1] => Ticket_Reply_25500
        )

)

Edited:

`unique_multidim_array($result ,'1');` 

This Function is passing a two params. one is array and another one is the key for the Unique values.

In explanation of checking the array key value is already exist or not

if(!in_array($val[$key], $key_array))

If that Value and key not in array going to return the array else it ejects like:

    $temp_array[$i] = $val;
return $temp_array;

You can change the Key for your convenience like 'Numeric key' or 'String key' like unique_multidim_array($result ,'a'); or unique_multidim_array($result ,'b');.

Nawin
  • 1,653
  • 2
  • 14
  • 23
  • You misread his data. The 3rd array element 2 is `Ticket_Reply_25500` and you have `TicketID_25500`. As I said previously in another comment.. `array_unique` will not work with this data – IsThisJavascript Mar 05 '18 at 10:33
  • Good, output is solid too. Well done. Might want to go into more detail about what you've done tho as this could be quite confusing method if you're new to PHP – IsThisJavascript Mar 05 '18 at 10:41
  • @Nawin Thank you much! Would you mind adding a few comments to your code and an example how to call the function unique_multidim_array($array, $key) using both TicketID and Ticket_Reply. – lin web Mar 05 '18 at 11:24
  • @Nawin I'm marking this as the accepted answer. My array is actually quite a bit more complicated, but this worked well during initial testing. – lin web Mar 05 '18 at 11:33
  • I don't know well English But i tried my best for the explanation @linweb – Nawin Mar 05 '18 at 11:42
  • @Nawin Your added comments helped me a lot. Thank you for adding the extra info. – lin web Mar 05 '18 at 11:44
1

Hope this can help you.

<?php

$a = array( 
    array("2018-03-03 07:43:15","TicketID_25500"),
    array("2018-03-03 08:00:00","TicketID_25500"),
    array("2018-03-03 08:00:00","Ticket_Reply_25500"),
    array("2018-03-03 08:03:00","Ticket_Reply_25500"),
);

function array_multi_unique($multiArray){
    $all = array_column($multiArray,1); // pass 0 for timestamp
    $unique = array_values(array_unique($all));

    foreach($unique as $key){
        $i = 0;
        foreach($multiArray as $k => $v){
            if(in_array($key,$v)){
                if($i != 0){
                    unset($multiArray[$k]);
                }
                $i++;
            }
        }
    }

    $multiArray = array_values($multiArray);
    return $multiArray;
}

$unique = array_multi_unique($a);
print_r($unique);
Ashish Tiwari
  • 1,919
  • 1
  • 14
  • 26
0

I think you need to

  1. Define a class (assume A) that implements Hashtable so that you have your own equals method. The data members of this class will be ticket id and timestamp.
  2. Instantiate a php set 'uniqueEntries'.
  3. Instantiate an array 'duplicateIndexes'.
  4. For each item in your array
    • Iterate through the array to create an instance of class A, with ticket id and timestamp of the object.
    • Check if the Set contains this object.
    • If it does not, add this new object in the Set.
    • If it contains the same object, then note the current iteration count in the duplicateIndexes array.
  5. Now remove the elements from array whose index exists in duplicateIndexes.
Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
  • I prefer a method without using a class unless a full example is provided as I'm not proficient in PHP:Classes. Thank you much though, I will research this more. – lin web Mar 05 '18 at 11:10