← Back to all snippets
PHP

Remove Duplicate Associative Arrays by a Specific Key

Learn to efficiently remove duplicate entries from an array of associative arrays in PHP, identifying uniqueness based on the value of a chosen key while preserving order of first occurrences.

<?php
function removeDuplicateAssociativeArraysByKey(array $array, string $key): array
{
    $seenKeys = [];
    $result = [];
    foreach ($array as $item) {
        if (!isset($item[$key])) {
            // Decide how to handle items without the key; for simplicity, we'll keep them as unique
            $result[] = $item;
            continue;
        }
        $keyValue = $item[$key];
        if (!in_array($keyValue, $seenKeys)) {
            $seenKeys[] = $keyValue;
            $result[] = $item;
        }
    }
    return $result;
}

// Alternative using array_filter for potentially better performance/memory for very large arrays
function removeDuplicateAssociativeArraysByKeyEfficient(array $array, string $key): array
{
    $seenKeys = [];
    return array_filter($array, function($item) use (&$seenKeys, $key) {
        if (!isset($item[$key])) {
            return true; // Keep items without the key or define specific logic
        }
        if (in_array($item[$key], $seenKeys)) {
            return false; // Duplicate, filter out
        }
        $seenKeys[] = $item[$key];
        return true; // Not seen before, keep
    });
}


$users = [
    ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Bob', 'email' => '[email protected]'],
    ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]'], // Duplicate ID
    ['id' => 3, 'name' => 'Charlie', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Robert', 'email' => '[email protected]'] // Duplicate ID, different name
];

echo "Original users:
";
print_r($users);

echo "
Unique users by 'id' (first occurrence):
";
print_r(removeDuplicateAssociativeArraysByKey($users, 'id'));

$products = [
    ['sku' => 'A101', 'name' => 'Laptop', 'price' => 1200],
    ['sku' => 'B202', 'name' => 'Mouse', 'price' => 25],
    ['sku' => 'A101', 'name' => 'Gaming Laptop', 'price' => 1500], // Duplicate SKU
    ['sku' => 'C303', 'name' => 'Keyboard', 'price' => 75]
];

echo "
Unique products by 'sku' (first occurrence, efficient method):
";
print_r(removeDuplicateAssociativeArraysByKeyEfficient($products, 'sku'));
?>
How it works: This snippet provides two methods to remove duplicate associative arrays from a list, based on the value of a specified key. The `removeDuplicateAssociativeArraysByKey` function iterates through the array, storing unique key values seen so far and only adding the first occurrence of an item with that key to the result. The `removeDuplicateAssociativeArraysByKeyEfficient` uses `array_filter` with a closure and a reference to an array of seen keys, which can be more memory-efficient for very large arrays. This is crucial for data cleaning and ensuring uniqueness in datasets.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs