1

How do I properly make an array like this one to be unique ? (and by unique I mean that only the array[3] is removed as it's a duplicate of array[2])

array(
 array( "Title", 50, 96 ),
 array( "Other Title", 110, 225 ),
 array( "Title", 110, 225 ),
 array( "Title", 110, 225 ),
)

The only array I want in this case to be filtered out is the last one (in this case), but there is no particular order, and manual labour isn't going to cut it. (popping a targeted position). It is a rather large array, I have to think about performance here as well.

I could also arrange the data to be something like this:

array(
 array( "Title" => array( 50, 96 ), array(110, 225) )
 array( "Other Title", array( 110, 225 ) )
)

But that doesn't help my cause this time.

There is no unique value in the array (alone), but the value combination for each second level array is unique.

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
pyronaur
  • 3,515
  • 6
  • 35
  • 52

4 Answers4

4

Try this.....

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Josh
  • 8,082
  • 5
  • 43
  • 41
rajmohan
  • 1,618
  • 1
  • 15
  • 36
2

Surely someone will beat me w/ a better performing solution, but this is a simple one. All you do is 'hash' the original arrays by concatenating all their elements, then keeping track of which ones have been added to the set of unique inputs. You only add an original array if its 'hashed' (concatenated values) have not yet been added to the hash array.

$aMain = array(
 array( "Title", 50, 96 ),
 array( "Other Title", 110, 225 ),
 array( "Title", 110, 225 ),
 array( "Title", 110, 225 ),
);

$_aHashed = array();
$aUniqued = array();
foreach($aMain as $aUnhashed) {
    $sHash = implode('', $aUnhashed);
    if(!in_array($sHash, $_aHashed)) {
        $_aHashed[] = $sHash;
        $aUniqued[] = $aUnhashed;
    }
}

var_dump($aUniqued);
quickshiftin
  • 66,362
  • 10
  • 68
  • 89
1
$filtered = array();
foreach ($array as $entry) {
    $hash = md5(serialize($entry));
    $filtered[$hash] = $entry;
}

You should even be able to leave off the md5 to speed up the process.

deceze
  • 510,633
  • 85
  • 743
  • 889
1
$array = array(array("Title", 50, 96), array("Other Title", 110, 225), array("Title", 110, 225), array("Title", 110, 225));

function remove_duplicates_multi_array($array) {

    $serialized_arrays = array();
    // serialize every array
    foreach ($array as $key=>$value) {
        $serialized_arrays[$key] = serialize($value);
    }
    // use array_unique to remove duplicates
    $unique_array = array_unique($serialized_arrays);

    $multi_unique = array();

    //unserialize every item inside the array
    foreach($unique_array as $key=>$value) {
        $multi_unique[$key] = unserialize($value);
    }

    return $multi_unique;
}

var_dump(remove_duplicates_multi_array($array));
busypeoples
  • 737
  • 3
  • 6