0

From following array key with ID 16 is comming twice. Hown can we remove this this duplicate ID. (REMOVE IF ID IS DUPLICATE OTHER FIELDS IGNORE)

 Array
   (
     [1] => Array
            (
                [ID] => 16
                [username] => dudda
                [message-time] => 2016-08-25 12:12:53
            )

   [2] => Array
            (
                [ID] => 16
                [username] => dudda
                [message-time] => 2016-08-25 12:01:54
            )

  [3] => Array
            (
                [ID] => 3
                [username] => himanshu
                [message-time] => 2016-08-15 12:53:38
            )

  [4] => Array
            (
                [ID] => 15
                [username] => dawinder
                [message-time] => 2016-08-10 11:40:33
            )
    )

3 Answers3

3

I Got solution

I have develop this function for same :

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; 

}

Now, call this function anywhere from your code,

something like this,

$details = unique_multidim_array($array_name,'key'); 
1

We used this to de-duplicate results from a variety of overlapping queries.

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
jmattheis
  • 10,494
  • 11
  • 46
  • 58
Senthil
  • 11
  • 1
1
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

Read this:http://php.net/manual/en/function.array-unique.php

For Example

  <?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

Output will be:

Array
(
    [a] => green
    [0] => red
    [1] => blue
)
  • This is more of a comment. Quoting the manual is not considered a good answer. Please provide a code example next time. The code you provided is the documentation definition of the function you linked – Machavity Aug 25 '16 at 13:31
  • Bro I know but when i answered it i was not able to comment(my reputation was less than 50) so i cant comment it. – Jayendra Manek Aug 25 '16 at 13:33
  • Provide a code example and this is an acceptable answer – Machavity Aug 25 '16 at 13:35