0

多次元配列があるとしましょう。例えば:

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

そして、私はすべてのヘビをヘビと比較し、すべてのパンダをパンダなどと比較し(パンダに対するヘビではありません)、一意の要素を配列に入れます(すべての一意の要素を単一の配列に入れます)、どうすればよいですか?これをやってみてください。それらは配列内の要素であるため、私は少し困惑しています。また、何種類あるのか事前にはわかりません。たとえば、あるときはパンダ、クマ、ヘビの多次元配列を渡すことができました。次回は鳥、猫、パンダ、ゼブラの配列を渡すことができました。

何か案は?

最終出力

      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 )    

     )
4

3 に答える 3

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

ご覧のとおり、これは動物の種類と名前を一意の識別子として使用します。あなたの質問は、動物がユニークである理由を述べていなかったので、あなたのニーズに合うように上記を変更してください。

于 2012-08-19T23:12:27.297 に答える
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.
        }
    }
}

必要な比較の種類を指定すると、この回答を改善できます。

于 2012-08-19T22:10:45.443 に答える
1

タイプ別に並べ替えてみてはいかがでしょうか:

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

どのような比較をしたいのかわかりませんが、これにより、少なくともタイプごとに個々のサブリストが取得されます。

于 2012-08-19T21:59:09.697 に答える