Risposte:
Interseca i bersagli con il pagliaio e assicurati che l'intersezione sia esattamente uguale ai bersagli:
$haystack = array(...);
$target = array('foo', 'bar');
if(count(array_intersect($haystack, $target)) == count($target)){
// all of $target is in $haystack
}
Nota che devi solo verificare che la dimensione dell'intersezione risultante sia la stessa dimensione dell'array di valori di destinazione per dire che $haystackè un superset di $target.
Per verificare che almeno un valore in $targetsia anche in $haystack, puoi eseguire questo controllo:
if(count(array_intersect($haystack, $target)) > 0){
// at least one of $target is in $haystack
}
Come sviluppatore, dovresti probabilmente iniziare ad imparare le operazioni sugli insiemi (differenza, unione, intersezione). Puoi immaginare il tuo array come un "insieme" e le chiavi che stai cercando per l'altro.
function in_array_all($needles, $haystack) {
return empty(array_diff($needles, $haystack));
}
echo in_array_all( [3,2,5], [5,8,3,1,2] ); // true, all 3, 2, 5 present
echo in_array_all( [3,2,5,9], [5,8,3,1,2] ); // false, since 9 is not present
function in_array_any($needles, $haystack) {
return !empty(array_intersect($needles, $haystack));
}
echo in_array_any( [3,9], [5,8,3,1,2] ); // true, since 3 is present
echo in_array_any( [4,9], [5,8,3,1,2] ); // false, neither 4 nor 9 is present
Uscendo dalla risposta di @Rok Kralj (migliore IMO) per verificare se esiste uno qualsiasi degli aghi nel pagliaio, è possibile utilizzarlo al (bool)posto del !!quale a volte può creare confusione durante la revisione del codice.
function in_array_any($needles, $haystack) {
return (bool)array_intersect($needles, $haystack);
}
echo in_array_any( array(3,9), array(5,8,3,1,2) ); // true, since 3 is present
echo in_array_any( array(4,9), array(5,8,3,1,2) ); // false, neither 4 nor 9 is present
IMHO La soluzione migliore di Mark Elliot per questo problema. Se hai bisogno di effettuare operazioni di confronto più complesse tra gli elementi dell'array E sei su PHP 5.3, potresti anche pensare a qualcosa di simile a quanto segue:
<?php
// First Array To Compare
$a1 = array('foo','bar','c');
// Target Array
$b1 = array('foo','bar');
// Evaluation Function - we pass guard and target array
$b=true;
$test = function($x) use (&$b, $b1) {
if (!in_array($x,$b1)) {
$b=false;
}
};
// Actual Test on array (can be repeated with others, but guard
// needs to be initialized again, due to by reference assignment above)
array_walk($a1, $test);
var_dump($b);
Ciò si basa su una chiusura; la funzione di confronto può diventare molto più potente. In bocca al lupo!
if(empty(array_intersect([21,22,23,24], $check_with_this)) {
print "Not found even a single element";
} else {
print "Found an element";
}
array_intersect () restituisce un array contenente tutti i valori di array1 presenti in tutti gli argomenti. Nota che le chiavi vengono conservate.
Restituisce un array contenente tutti i valori in array1 i cui valori esistono in tutti i parametri.
empty () - Determina se una variabile è vuota
Restituisce FALSE se var esiste e ha un valore non vuoto, diverso da zero. Altrimenti restituisce VERO.