Jump to content

Duplicates removed from a multidimensional array attending to one value


LLeoun

Recommended Posts

Hi all,

 

 

How can be duplicates removed from a multidimensional array attending to just one value of the element?

Here an example (taken from a post opened here time ago) of what I mean :

 

The following code will show the array without the last             

'ID' => 22222,

'Name' => 'Mike'

 

<?php
$result = array
      (
          'SearchContentByEverythingResult' => array
              (
                  'Content' => array
                      (
                          0 => array
                              (
                                  'ID' => 11111,
                                  'Name' => 'Bill'
                                  ),
                          1 => array
                              (
                                  'ID' => 22222,
                                  'Name' => 'Mike'
                                  ),
                          2 => array
                              (
                                  'ID' => 33333,
                                  'Name' => 'Sam'
                                  ),
                          3 => array
                              (
                                  'ID' => 22222,
                                  'Name' => 'Mike'
                                  )
                     )
              )
      );

/**
* remove dupes
*/
$a = $result['SearchContentByEverythingResult']['Content'];
$k = count($a);
for($i=0; $i < $k-1; $i++)
{
    for($j=$i+1; $j < $k; $j++)
    {
        if ($a[$i] == $a[$j]) unset($a[$j]);
    }
}
$result['SearchContentByEverythingResult']['Content'] = $a;
echo '<pre>', print_r($result, true), '</pre>';
?>

 

But if the $result is composed like this:

 

 

$result = array
      (
          'SearchContentByEverythingResult' => array
              (
                  'Content' => array
                      (
                          0 => array
                              (
                                  'ID' => 11111,
                                  'Name' => 'Bill'
                                  ),
                          1 => array
                              (
                                  'ID' => 22222,
                                  'Name' => 'Mike'
                                  ),
                          2 => array
                              (
                                  'ID' => 33333,
                                  'Name' => 'Sam'
                                  ),
                          3 => array
                              (
                                  'ID' => 44444,
                                  'Name' => 'Mike'
                                  )
                     )
              )
      );

 

where the repetition is in the name, not in the ID .. the code will show two Mikes.

Using this last version of $result how can I remove index 3, how can I have just one Mike no matter what his ID is??

 

Thanks a ton!

Instead of trying to manipulate the array here, can you tell us how the array is being constructed... is it from a database? There is a chance you can modify code elsewhere to make this more efficient.

Just in case somebody needs it, the way to do it is:

 

<?php
$result = array
      (
          'SearchContentByEverythingResult' => array
              (
                  'Content' => array
                      (
                          0 => array
                              (
                                  'ID' => 11111,
                                  'Name' => 'Bill'
                                  ),
                          1 => array
                              (
                                  'ID' => 22222,
                                  'Name' => 'Mike'
                                  ),
                          2 => array
                              (
                                  'ID' => 33333,
                                  'Name' => 'Sam'
                                  ),
                          3 => array
                              (
                                  'ID' => 44444,
                                  'Name' => 'Mike'
                                  )
                     )
              )
      );



$a = $result['SearchContentByEverythingResult']['Content'];
$k = count($a);

for($i=0; $i < $k-1; $i++)
{
        if ( false == isset( $a[$i] ) )
        {
                continue;
        }               
        
    for($j=$i+1; $j < $k; $j++)
    {
                if ( false == isset( $a[$j] ) )
                {
                        continue;
                }               
                
        if ( $a[$i]['ID'] == $a[$j]['ID'] || $a[$i]['Name'] == $a[$j]['Name'] )
                {
                        unset($a[$j]);
                }
    }
}

$result['SearchContentByEverythingResult']['Content'] = $a;
echo '<pre>', print_r($result, true), '</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.