Comparing elements in a multidimensional array

后端 未结 3 593
谎友^
谎友^ 2020-12-20 09:20

Say i have a multidimensional array. For example:

 Array ( 
         [0] => Array ( 
             [animal_id] => 5494 
             [animal_name] =>         


        
相关标签:
3条回答
  • 2020-12-20 10:14

    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.

    0 讨论(0)
  • 2020-12-20 10:18

    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.

    0 讨论(0)
  • 2020-12-20 10:25

    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.

    0 讨论(0)
提交回复
热议问题