比较多维数组中的元素

时间:2012-08-19 21:55:43

标签: php multidimensional-array

说我有一个多维数组。例如:

 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 )             
         )

我想比较所有的蛇与蛇,以及所有熊猫对抗熊猫等(但不是对熊猫的蛇)并将独特的元素放入一个数组(所有独特的元素组成一个数组),我该怎么做呢?因为它们是数组中的元素,所以我有点难过。此外,我不会事先知道会有多少种不同的类型。例如,有一次我可以通过Panda,Bear,Snake传递一个多维数组 - 下次我可以通过一个包含Bird,Cat,Panda,Zebra的数组。

有什么想法吗?

最终输出

      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 )    

     )

3 个答案:

答案 0 :(得分:3)

数组键必须是唯一的,因此我们可以利用它。

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);

提供以下数组

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,
  ),
)

如您所见,这会将动物的类型和名称作为唯一标识符。你的问题没有说明动物的独特之处,所以改变上面的内容以满足你的需要。

答案 1 :(得分:1)

如何按类型对它们进行排序:

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

我不确定你想做什么样的比较,但这至少可以按类型获得个人子列表。

答案 2 :(得分:1)

您可以遍历数组,然后交叉检查每个项目:

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.
        }
    }
}

如果您指定了您想要的比较类型,我可以用这个来改进这个答案。