Removing Duplicate Keys from Multiple Arrays

Posted on 2011-04-25
Last Modified: 2012-05-11
array_unique can be used to remove duplicate key value sets from an array.

How do I remove duplicate key values sets by comparing multiple arrays?
Question by:deharvy
    LVL 39

    Accepted Solution


    $arr = array_map('unserialize', array_unique(array_map('serialize', $arr)));

    LVL 107

    Expert Comment

    by:Ray Paseur
    Give us some example inputs and expected outputs.  It matters whether the array keys are numeric or associative.
    LVL 107

    Expert Comment

    by:Ray Paseur
    The exact definition of "remove" also matters.  Do you want to eliminate duplicates from all of the arrays?  Or do you want to keep only one copy in one of the arrays?  Do you want to test for duplicate keys, or duplicate values or duplicate key=>value pairs?  

    Author Comment


    I only want to test for duplicate values. The key doesn't matter.

    So, when I do a print_r, it should only display one key with a unique value.

    Hope this is more clear.
    LVL 107

    Expert Comment

    by:Ray Paseur
    Please give us some example inputs and expected outputs, thanks.  Like how many arrays are involved, and whether the keys are associative or numeric?  Thanks, ~Ray

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now