PHP Duplicate search | return new array but maintain keys
I have an array of objects. In this array, there are duplicate values. I would like to find the duplicates in the array, place them in a new array, remove the original, and then get print out information from th开发者_C百科e first array where the key from the new array is the same.
For instance:
Array 1
[0] 
     [0] => ID: 123
     [1] => 0fac686d86aba414411b58f6bce53a76
[1] 
     [0] => ID: 124
     [1] => 0134b04a942cbc5336958c8cd09b82f3
[2] 
     [0] => ID: 125
     [1] => 0fac686d86aba414411b58f6bce53a76
[3] 
     [0] => ID: 126
     [1] => 0fac686d86aba414411b58f6bce53a76
Array 2
[0]
    [0] => ID: 125
    [1] => ID: 126
I know I can do something like to find the duplicates:
    function array_not_unique( $array1 is array() )
{
  return array_diff_key( $array1 , array_unique( $a ) );
}
But my hangup is, going back into the the first array and pulling out the IDs that match the duplicates.
For those that might need to know: I am parsing an RSS feed and finding the MD5 of a node. That is how I am determining uniqueness.
Thank you in advance.
If those IDs are unique, why not make them the key of your original array, it'll be much easier to work with:
[123] => 0fac686d86aba414411b58f6bce53a76
[124] => 0134b04a942cbc5336958c8cd09b82f3
Is this what you mean?
    $a = array(
            123 => '0fac686d86aba414411b58f6bce53a76',
            124 => '0134b04a942cbc5336958c8cd09b82f3',
            125 => '0fac686d86aba414411b58f6bce53a76',
            126 => '0fac686d86aba414411b58f6bce53a76'
    );
    $values = array();
    $duplicate_keys = array();
    foreach ($a as $key => $value)
    {
            if (in_array($value, $values))
            {
                    $duplicate_keys[] = $key;
            }
            $values[] = $value;
    }
    foreach ($duplicate_keys as $id)
    {
            echo "Duplicate " . $id . ", " . $a[$id];
    }
Have you looked at PHP's array_unique() function?  It maintains keys.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论