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
            )
    )
2
  • what language is this? also note that the message times are different, are you sure this is a duplicate? Commented Aug 25, 2016 at 10:27
  • What language is that ? I suppose this is PHP ? Commented Aug 25, 2016 at 10:27

3 Answers 3

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

Comments

1

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

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

1 Comment

here message-time is different, we have to remove on the base of ID only.
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
)

3 Comments

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
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.
Provide a code example and this is an acceptable answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.