21

Not sure if this question is a duplicate in need of removal, but I couldn't find the answer elsewhere so I'll have a go at asking.

I've got a 2d array that looks as follows:

Array ( [0] => Array ( [0] => dave [1] => jones [2] => [email protected] ) [1] => Array ( [0] => john [1] => jones [2] => [email protected] ) [2] => Array ( [0] => bruce [1] => finkle [2] => [email protected] ) ) 

I'd like to remove those with duplicate emails. So in the above example I'd like to just remove either [][0] or [][2]. I'm not worried about checking against names or anything like that, I just need the sub arrays to be deduplicated based on a single value.

At the moment I have something like this

 if(is_array($array) && count($array)>0){ foreach ($array as $subarray) { $duplicateEmail[$subarray[2]] = isset($duplicateEmail[$subarray[2]]); unset($duplicateEmail[$subarray[2]]); } } 

but it just ain't right. Any help appreciated.

0

6 Answers 6

29

A quick solution which uses the uniqueness of array indexes:

$newArr = array(); foreach ($array as $val) { $newArr[$val[2]] = $val; } $array = array_values($newArr); 

Notice 1: As visible from above, the last match for an email address is used instead of the first. This can be changed by replacing the second line with

foreach (array_reverse($array) as $val) { 

Notice 2: The indexes in the resulting array are somewhat mixed up. But I guess this doesn't matter...

Sign up to request clarification or add additional context in comments.

3 Comments

your 'for' should have been a 'foreach' but otherwise it works well, thanks.
@Cassy - Just turned this into a function for myself in a little app I'm coding, thanks for it!
Thanks man that did the trick .Don't know why i missed this answer at first Now issue solved! :)
15

Much Simpler Solution.

$unique = array_map('unserialize', array_unique(array_map('serialize', $array))); echo "<pre>"; print_r($unique); 

1 Comment

That doesn't answer the question. That just checks for unique arrays rather than their properties. Appreciate you giving it another go though.
8

The user comments for array_unique() have a few solutions to this. For example

 function multi_unique($array) { foreach ($array as $k=>$na) $new[$k] = serialize($na); $uniq = array_unique($new); foreach($uniq as $k=>$ser) $new1[$k] = unserialize($ser); return ($new1); }

from http://uk.php.net/manual/en/function.array-unique.php#57202.

1 Comment

which covers the whole sub-array rather than individual values if i'm not mistaken
3
$array = array( array('dave','jones','[email protected]'), array('dave','jones','[email protected]'), array('dave','jones','[email protected]'), array('dave','jones','[email protected]'), array('dave','jones','[email protected]') ); $copy = $array; // create copy to delete dups from $usedEmails = array(); // used emails for( $i=0; $i<count($array); $i++ ) { if ( in_array( $array[$i][2], $usedEmails ) ) { unset($copy[$i]); } else { $usedEmails[] = $array[$i][2]; } } print_r($copy); 

Comments

1

User SORT_REGULAR as second parameter.

$uniqueArray = array_unique($array, SORT_REGULAR); 

Comments

0

My proposition:

protected function arrayUnique($array, $preserveKeys = false) { $uniqueArray = array(); $hashes = array(); foreach ($array as $key => $value) { if (true === is_array($value)) { $uniqueArray[$key] = $this->arrayUnique($value, $preserveKeys); } else { $hash = md5($value); if (false === isset($hashes[$hash])) { if ($preserveKeys) { $uniqueArray[$key] = $value; } else { $uniqueArray[] = $value; } $hashes[$hash] = $hash; } } } return $uniqueArray; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.