Viewed   97 times

I have associative array like below

$arr = [1=>0, 2=>1, 3=>1, 4=>2]

I would like to remove the duplicate values from the initial array and return those duplicates as a new array. So I would end up with something like;

$arr = [1=>0, 4=>2]

$new_arr = [2=>1, 3=>1]

Does PHP provide such a function or if not how would I achieve this?




Use array_filter() to get all duplicate values from array

Use array_diff() to get all unique values from array

$array = array(1=>0, 2=>1, 3=>1, 4=>2);
$counts = array_count_values($array);
$duplicates = array_filter($array, function ($value) use ($counts) {
    return $counts[$value] > 1;
print '<pre>';print_r($duplicates);

print '<pre>';print_r($result);


    [2] => 1
    [3] => 1

    [1] => 0
    [4] => 2
Monday, October 10, 2022

Use array_unique().


$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
Monday, October 3, 2022

The user comments on the array_unique page do shed some light on this. You will most likely find some hidden gems in those comments - its a very handy documentation.

Just a quick browser through revealed the following to remove duplicates from a multi dimensional array:

function super_unique($array)
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

  foreach ($result as $key => $value)
    if ( is_array($value) )
      $result[$key] = super_unique($value);

  return $result;
Saturday, September 10, 2022

i asume your json comes via ajax.... (otherwise the code works with json_decode) so be sure the js json stringifys your object and

you'll need to stripslashes before json_decode ;-) in php

Sunday, November 13, 2022
$value = reset($arr);
$key = key($arr);

(in that order)

See reset()PHP Manual, key()PHP Manual.

unset($arr[$key]); # in case you want to remove it.

However array_pop()PHP Manual is working with the last element:

$value = end($arr);
$key = key($arr);
unset($arr[$key]); # in case you want to remove it.

See end()PHP Manual.

For the fun:

list($value, $key) = array(end($arr), key($arr));


extract(array('value'=>end($arr), 'key'=>key($arr)));


list($key, $value) = each($arr);

or whatever style of play you like ;)

Dealing with empty arrays

It was missing so far to deal with empty arrays. So it's a need to check if there is a last (first) element and if not, set the $key to null (as null can not be an array key):


This will give for a filled array like $arr = array('first' => '1st', 'last' => '2nd.');:

string(4) "2nd." # value
string(4) "last" # key
array(1) { # leftover array
  string(3) "1st"

And an empty array:

bool(false) # value
NULL # key
array(0) { # leftover array

Afraid of using unset?

In case you don't trust unset() having the performance you need (of which I don't think it's really an issue, albeit I haven't run any metrics), you can use the native array_pop() implementation as well (but I really think that unset() as a language construct might be even faster):

$key = key($arr);
$value = array_pop($arr);
Thursday, August 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :