Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working with a multi-dimensional array. How can I remove duplicates by value? In the following array, [0], [2] and [5] have the same [ID]. Is there a function that will remove any duplicate arrays based on a particular value? In this case, I would like to remove array [2] and array [5], as they have the same [ID] as array [0].

Thank you for any information you may have.

        Array
(
    [0] => stdClass Object
        (
            [d] => 2010-10-18 03:30:04
            [ID] => 9
        )

    [1] => stdClass Object
        (
            [d] => 2010-10-18 03:30:20
            [ID] => 4
        )

    [2] => stdClass Object
        (
            [d] => 2010-11-03 16:46:34
            [ID] => 9
        )

    [3] => stdClass Object
        (
            [d] => 2010-11-02 03:19:14
            [ID] => 1
        )

    [4] => stdClass Object
        (
            [d] => 2010-05-12 04:57:34
            [ID] => 2
        )    

    [5] => stdClass Object
        (
            [d] => 2010-05-10 05:24:15
            [ID] => 9
        )

)
share|improve this question
FYI, you keep using the term "key", but you area actually referring to a "value", not a "key". The [0], [2], and [5] you mention are keys. The [ID] is also a key. The 9 that each of these ID's contains are all values. – Matt Huggins Dec 1 '10 at 22:28
thanks, i have modified the language of the question. – superUntitled Dec 1 '10 at 22:42
add comment (requires an account with 50 reputation)

4 Answers

up vote 3 down vote accepted

One way to do it: ($old_array is your array, and $new_array will contain the new one, dupes removed, keyed by that ID)

$new_array = array();
foreach ($old_array as $item)
  if (!array_key_exists($item->ID, $new_array))
    $new_array[$item->ID] = $item;

(I haven't tested this, but it should work)

share|improve this answer
add comment (requires an account with 50 reputation)

I think it would be easiest to loop through and construct a new array. In the loop, use array_key_exists() to determine if the ID already exists in the new array, and if not, append an element.

share|improve this answer
add comment (requires an account with 50 reputation)

It is not a multidimensional array, it is an array of objects. You can just loop over it (this example changes the array in place):

$ids = array();

forach($array as $key=>$obj) {
    if(isset($ids[$obj->ID])) {//did we already encounter an object with this ID?
        unset($array[$key]); // if yes, delete this object
    }
    else {
        $ids[$obj->ID] = 1; // if not, add ID to processed IDs
    }
}

You can also create a new array and add the objects to the new array.

share|improve this answer
add comment (requires an account with 50 reputation)
foreach ($array as $element) {
    foreach ($array as &$element2) {
        if ($element2->ID == $element->ID && $element2 != $element) {
            unset($element2);
        }
    }
}

Note the loop-by-reference in the second foreach.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.