2017-08-10 1 views
0

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'; 
} 

Répondre

0
$buildingId = 2; 
$typeId = 2; 
$result = false; 
foreach ($array as $key => $val) { 
    if ($val['buildingId'] == $buildingId && $val['typeId'] == $typeId) { 
     $result = $key; // If you want the key in the array 
     $result = $val; // If you want directly the entry you're looking for 
     break; // So that you don't iterate through the whole array while you already have your reuslt 
    } 
} 
+0

Merci beaucoup: D cela fonctionne! –

+0

si je voudrais savoir que la combinaison typeId = 17 et buildingId = 1 n'existe pas, comment ferais-je cela? –

+0

@LoesVisser Si aucun résultat n'est trouvé, '$ result' évalue à' false' après le 'foreach'. – ksjohn

1

Vous pouvez essayer ce code:

$keyTypeExistsAndHaveSameValue = isset($array['typeId']) && $array['typeId'] === 2; 
$keyBuildingExistsAndHaveSameValue = isset($array['buildingId']) && $array['buildingId'] === 2; 

if($keyTypeExistsAndHaveSameValue && $keyBuildingExistsAndHaveSameValue){ 
    echo 'Combination does exists' 
} 

Cette vérification de code si typeId & clés buildingId existent mais aussi vérifier si ses valeurs sont 2 et 2.

0

Je pense que ce que vous besoin est ce

$keyType = array_search(1, array_column($array, 'typeId')); 
$keyBuilding = array_search(17, array_column($array, 'buildingId')); 

if(is_numeric($keyType)||is_numeric($keyBuilding)){ 
    echo 'Combination does exists'; 
} 

Maintenant, ici vous avez besoin ou opérateur inste ad de et operator parce que vous voulez que typeid = 1 existe ou que l'id de construction = 17 existe.

Si j'ai bien compris votre question, alors vous essayez de faire quelque chose comme ça, n'est-ce pas?

Espérons que cela aide!

0

Vous devez faire une boucle foreach pour obtenir le nombre réel de tableau, l'autre solution ne semble pas répondre à ce que vous cherchez, ce qui est d'obtenir le numéro d'index.

Je voudrais rechercher si la combinaison, par exemple, [buildingId] => 2, [typeId] => 2 existe est un tableau 0, 1 ou 2.

EDIT: Ce le code est un code juste exemple pour montrer comment vous obtiendrez le numéro d'index de tableau, dans un environnement de production, vous économiseriez les tableaux correspondants, les chiffres de comparaison ne sont pas codés en dur, etc ...

$array = array(array('id' => 2, 'zoneId' => 2, 'buildingId' => 2, 'typeId' => 2), 
      array('id' => 4, 'zoneId' => 2, 'buildingId' => 2, 'typeId' => 2), 
      array('id' => 6, 'zoneId' => 6, 'buildingId' => 17, 'typeId' => 2)); 


foreach ($array as $building => $building_details) 
{ 
    if ($building_details['buildingId'] === 2 && $building_details['typeId'] === 2) 
    { 
     echo 'Array number ' . $building . ' matches criteria<br>'; 
    } 
} 

sortie:

Array number 0 matches criteria 
Array number 1 matches criteria 

Vous pouvez afficher cet extrait en ligne here.