Comparing elements in a multidimensional array

后端 未结 3 615
谎友^
谎友^ 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: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.

提交回复
热议问题