Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

php - Comparing elements in a multidimensional array

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 )    

     )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...