Filter out duplicate items in an associative array in PHP

Associative arrays in PHP offer a versatile way to store complex data structures. When it comes to filtering out duplicates based on a specific field within these arrays, the array_unique function might seem like a convenient solution. However, there's a catch.

The PHP manual entry for array_unique ends with the following remark:

Note: Note that array_unique() is not intended to work on multi dimensional arrays.

Imagine you have an array similar to this:

$array = [
	['id' => 1, 'name' => 'First'],
   	['id' => 2, 'name' => 'Second'],
	['id' => 3, 'name' => 'Third'],
	['id' => 1, 'name' => 'First'],

When you want to filter out the last item - as it's a duplicate of the first item - you simply can't use array_unique. How should we proceed?

The solution

This code snippet aims to filter out duplicates by focusing solely on values extracted from a specified field, disregarding the overall structure of the associative array:

function unique(array $array, string $field)
    return array_intersect_key(
            array_column($array, $field)

$deduplicated = unique($array, 'id');

It extracts the provided field from the provided array into a separate list of values. It deduplicates that list, and uses it as a "template" to diff out the duplicates items in the original array using their keys.

Hopefully somebody finds this useful!