Ecco perché true == 'hey'
a causa di type juggling. Quello che stai cercando è:
if (in_array($input, $arr['test'], true)) {
costringe un test di uguaglianza sulla base di ===
invece di ==
.
in_array('hey', array('soap' => true)); // true
in_array('hey', array('soap' => true), true); // false
Per capire il tipo di giocoleria meglio si può giocare con questo:
var_dump(true == 'hey'); // true (because 'hey' evaluates to true)
var_dump(true === 'hey'); // false (because strings and booleans are different type)
Aggiornamento
Se vuoi sapere se una chiave di serie è impostato (piuttosto che se un valore è presente), è necessario utilizzare isset()
in questo modo:
if (isset($arr['test'][$input])) {
// array key $input is present in $arr['test']
// i.e. $arr['test']['hey'] is present
}
Update 2
C'è anche array_key_exists()
che può testare per la serie presenza chiave; tuttavia, dovrebbe essere utilizzato solo se esiste la possibilità che il valore dell'array corrispondente possa essere null
.
if (array_key_exists($input, $arr['test'])) {
}
fonte
2012-12-16 12:35:56
Ah, capisco. Sta anche cercando i valori di ogni tasto dell'array. – John
@John In realtà ** solo ** guardando questi. – ComFreek
@ John Ho aggiornato la risposta, nel caso steste cercando qualcos'altro. –