Remove items from an associative array with two matching subsets

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

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

Editing additional information:

  • I always need to remove the submatrix with TicketID_xxxxx while saving the submatrix 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
        )
}
+4
source share
3 answers

You can use the option SORT_REGULAR. More doc HERE aboutarray_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:

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

. - , - .

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

, :

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

Key , " " " ", unique_multidim_array($result ,'a'); unique_multidim_array($result ,'b');.

+2

, .

<?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);
+1

,

  • ( A), Hashtable, equals. .
  • php set 'uniqueEntries'.
  • 'duplicateIndexes'.
    • , A .
    • , .
    • If not, add this new object to Set.
    • If it contains the same object, then pay attention to the current iteration counter in the duplicateIndexes array.
  • Now remove the elements from the array whose index exists in duplicateIndexes.
0
source

Source: https://habr.com/ru/post/1694465/


All Articles