0

Say i have a multidimensional array. For example:

 Array ( 
         [0] => Array ( 
             [animal_id] => 5494 
             [animal_name] => "Suzy"
             [animal_type] => "zebra" 
             [animal_location] => 0 
             [animal_awake] => 1
             [animal_age] => 3 ) 
         [1] => Array ( 
             [animal_id] => 5494 
             [animal_name] => "Joshua"
             [animal_type] => "panda"
             [animal_location] => 5
             [animal_awake] => 0
             [animal_age] => 8 )
        [2] => Array ( 
             [animal_id] => 5494 
             [animal_name] => "Debra"
             [animal_type] => "snake" 
             [animal_location] => 7 
             [animal_awake] => 1
             [animal_age] => 3 ) 
        [3] => Array ( 
             [animal_id] => 5495 
             [animal_name] => "Caleb"
             [animal_type] =>  "zebra"
             [animal_location] => 0
             [animal_awake] => 1
             [animal_age] => 3 ) 
        [4] => Array ( 
             [animal_id] => 5495 
             [animal_name] => "Joshua"
             [animal_type] =>  "panda"
             [animal_location] => 5 
             [animal_awake] => 0
             [animal_age] => 8 )    
        [5] => Array ( 
             [animal_id] => 5495 
             [animal_name] => "Debra"
             [animal_type] =>  "snake"
             [animal_location] => 7 
             [animal_awake] => 1
             [animal_age] => 3 ) 
        [6] => Array ( 
             [animal_id] => 5496 
             [animal_name] => "Emily"
             [animal_type] =>  "zebra"
             [animal_location] => 0
             [animal_awake] => 1
             [animal_age] => 3 ) 
        [7] => Array ( 
             [animal_id] => 5496 
             [animal_name] => "Joshua"
             [animal_type] =>  "panda"
             [animal_location] => 5 
             [animal_awake] => 0
             [animal_age] => 8 )    
        [8] => Array ( 
             [animal_id] => 5496 
             [animal_name] => "Debra"
             [animal_type] =>  "snake"
             [animal_location] => 7 
             [animal_awake] => 1
             [animal_age] => 3 )             
         )

And i want to compare all the snakes against the snakes, and all the panda's against the panda's etc (but not a snake against a panda) and put the unique elements into an array(all unique elements into a single array), how would i go about doing this. Since they are elements within an array, i'm a bit stumped. Also, i won't know in advance how many different types there will be. For instance, one time i could be passed an multidimensional array with Panda, Bear, Snake -- Next time i could be passed an array with Bird, Cat, Panda, Zebra.

Any ideas?

FINAL OUTPUT

      Array ( 
     [0] => Array ( 
         [animal_id] => 5494 
         [animal_name] => "Suzy"
         [animal_type] => "zebra" 
         [animal_location] => 0 
         [animal_awake] => 1
         [animal_age] => 3 ) 
     [1] => Array ( 
         [animal_id] => 5494 
         [animal_name] => "Joshua"
         [animal_type] => "panda"
         [animal_location] => 5
         [animal_awake] => 0
         [animal_age] => 8 )
    [2] => Array ( 
         [animal_id] => 5495 
         [animal_name] => "Caleb"
         [animal_type] =>  "zebra"
         [animal_location] => 0
         [animal_awake] => 1
         [animal_age] => 3 )    
    [3] => Array ( 
         [animal_id] => 5495 
         [animal_name] => "Debra"
         [animal_type] =>  "snake"
         [animal_location] => 7 
         [animal_awake] => 1
         [animal_age] => 3 ) 
    [4] => Array ( 
         [animal_id] => 5496 
         [animal_name] => "Emily"
         [animal_type] =>  "zebra"
         [animal_location] => 0
         [animal_awake] => 1
         [animal_age] => 3 )    

     )
BigBug
  • 6,202
  • 23
  • 87
  • 138
  • why the thumbs down? I couldn't figure out a way that doesn't take a lot of time - i'm dealing with a ton of data. If you're going to put a thumbs down, at least write a reason.... – BigBug Aug 19 '12 at 23:23
  • i thumbs upped it to negate the illogical thumbs down ;-) – Ovi Tisler Aug 21 '12 at 14:48
  • @OviTisler thanks.. that was kind of you, appreciate it – BigBug Aug 21 '12 at 15:43

3 Answers3

3

Array keys must be unique, so lets use that to our advantage.

function get_animal_key($animal) {
    return $animal['animal_type'] . '-' . $animal['animal_name'];
}

$uniques = array();
foreach ($array as $animal) {
    $key = get_animal_key($animal);
    $uniques[$key] = $animal;
}

var_export($uniques);

Gives the following array

array (
  'zebra-Suzy' => 
  array (
    'animal_id' => 5494,
    'animal_name' => 'Suzy',
    'animal_type' => 'zebra',
    'animal_location' => 0,
    'animal_awake' => 1,
    'animal_age' => 3,
  ),
  'panda-Joshua' => 
  array (
    'animal_id' => 5496,
    'animal_name' => 'Joshua',
    'animal_type' => 'panda',
    'animal_location' => 5,
    'animal_awake' => 0,
    'animal_age' => 8,
  ),
  'snake-Debra' => 
  array (
    'animal_id' => 5496,
    'animal_name' => 'Debra',
    'animal_type' => 'snake',
    'animal_location' => 7,
    'animal_awake' => 1,
    'animal_age' => 3,
  ),
  'zebra-Caleb' => 
  array (
    'animal_id' => 5495,
    'animal_name' => 'Caleb',
    'animal_type' => 'zebra',
    'animal_location' => 0,
    'animal_awake' => 1,
    'animal_age' => 3,
  ),
  'zebra-Emily' => 
  array (
    'animal_id' => 5496,
    'animal_name' => 'Emily',
    'animal_type' => 'zebra',
    'animal_location' => 0,
    'animal_awake' => 1,
    'animal_age' => 3,
  ),
)

As you can see, this takes the animal's type and name as the unique identifiers. Your question did not state what makes an animal unique, so alter the above to suit your needs.

salathe
  • 51,324
  • 12
  • 104
  • 132
1

How about sorting them by type:

$animalsByType = array();
foreach ($array as $animal) {
    $type = $animal['animal_type'];
    if (!isset($animalsByType[$type])) {
        $animalsByType[$type] = array();
    }
    $animalsByType[$type][] = $animal;
}

I'm not sure what kind of comparing you want to do, but this at least gets you individual sublists by type.

curtisdf
  • 4,130
  • 4
  • 33
  • 42
  • thanks, your response helps a bit. I want to compare and put all the unique arrays into a single array. By unique, i mean if one of the types has different values (not including the ID) into a single array. So in the array i posted above, all the Zebra types would enter into the array, but only one Panda would be in the array as they all contain the same values. – BigBug Aug 19 '12 at 22:05
  • I'm not sure I follow you still. The code I gave has an array for each unique type of animal present. Each value in the `$animalsByType` is an array contains a different type. – curtisdf Aug 19 '12 at 22:12
  • Maybe could you edit your question and put up an example of what the resulting array is intended to look like? – curtisdf Aug 19 '12 at 22:12
1

You could run through the array and then crosscheck every item:

foreach($aAnimals AS $iKey => $aAnimalData
{
    foreach($aAnimals AS $iSubKey => $aData)
    {
        if($aAnimalData['animal_type'] == $aData['animal_type'] && $iKey != $iSubKey)
        {
            // Start doing whatever you want to do when the types match.
            // The last part makes sure the second foreach does not 
            // match with the first one.
        }
    }
}

If you specify what kind of comparing you want I could improve this answer with that.

Deep Frozen
  • 2,053
  • 2
  • 25
  • 42