J'ai un tableau multidimensionnel:PHP tableau Multidimensional la recherche de combinaisons de valeurs dans le tableau
$array =
Array (
[0] => Array ([id] => 2 [zoneId] => 2 [buildingId] => 2 [typeId] => 2)
[1] => Array ([id] => 4 [zoneId] => 2 [buildingId] => 2 [typeId] => 1)
[2] => Array ([id] => 6 [zoneId] => 6 [buildingId] => 17 [typeId] => 2))
Et je voudrais rechercher si la combinaison, par exemple, [buildingId] => 2, [typeId] => 2 existe est un tableau 0, 1 ou 2.
J'ai essayé les éléments suivants:
$keyType = array_search(2, array_column($array, 'typeId'));
$keyBuilding = array_search(2, array_column($array, 'buildingId'));
if(is_numeric($keyType)&&is_numeric($keyBuilding)){
echo 'Combination does exists'
}
Cela fonctionne, mais donne aussi un faux positif si je rechercher [buildingId] => 17 [ typeId] => 1. Comment ca n je résous ça?
modifier
Je voudrais aussi savoir si une combinaison est pas dans le tableau, comment puis-je arranger cela?
if($result == false){
echo 'does not exists';
}
Merci beaucoup: D cela fonctionne! –
si je voudrais savoir que la combinaison typeId = 17 et buildingId = 1 n'existe pas, comment ferais-je cela? –
@LoesVisser Si aucun résultat n'est trouvé, '$ result' évalue à' false' après le 'foreach'. – ksjohn