Risposte:
Usando if?
if(isset($something['say']) && $something['say'] == 'bla') {
// do something
}
A proposito, stai assegnando un valore con la chiave saydue volte, quindi il tuo array risulterà in un array con un solo valore.
usando: in_array()
$search_array = array('user_from','lucky_draw_id','prize_id');
if (in_array('prize_id', $search_array)) {
echo "The 'prize_id' element is in the array";
}
Ecco l'output: The 'prize_id' element is in the array
usando: array_key_exists()
$search_array = array('user_from','lucky_draw_id','prize_id');
if (array_key_exists('prize_id', $search_array)) {
echo "The 'prize_id' element is in the array";
}
Nessuna uscita
In conclusione, array_key_exists()non funziona con un semplice array. È solo per scoprire se esiste o meno una chiave di matrice. Usoin_array() invece.
Ecco un altro esempio:
<?php
/**++++++++++++++++++++++++++++++++++++++++++++++
* 1. example with assoc array using in_array
*
* IMPORTANT NOTE: in_array is case-sensitive
* in_array — Checks if a value exists in an array
*
* DOES NOT WORK FOR MULTI-DIMENSIONAL ARRAY
*++++++++++++++++++++++++++++++++++++++++++++++
*/
$something = array('a' => 'bla', 'b' => 'omg');
if (in_array('omg', $something)) {
echo "|1| The 'omg' value found in the assoc array ||";
}
/**++++++++++++++++++++++++++++++++++++++++++++++
* 2. example with index array using in_array
*
* IMPORTANT NOTE: in_array is case-sensitive
* in_array — Checks if a value exists in an array
*
* DOES NOT WORK FOR MULTI-DIMENSIONAL ARRAY
*++++++++++++++++++++++++++++++++++++++++++++++
*/
$something = array('bla', 'omg');
if (in_array('omg', $something)) {
echo "|2| The 'omg' value found in the index array ||";
}
/**++++++++++++++++++++++++++++++++++++++++++++++
* 3. trying with array_search
*
* array_search — Searches the array for a given value
* and returns the corresponding key if successful
*
* DOES NOT WORK FOR MULTI-DIMENSIONAL ARRAY
*++++++++++++++++++++++++++++++++++++++++++++++
*/
$something = array('a' => 'bla', 'b' => 'omg');
if (array_search('bla', $something)) {
echo "|3| The 'bla' value found in the assoc array ||";
}
/**++++++++++++++++++++++++++++++++++++++++++++++
* 4. trying with isset (fastest ever)
*
* isset — Determine if a variable is set and
* is not NULL
*++++++++++++++++++++++++++++++++++++++++++++++
*/
$something = array('a' => 'bla', 'b' => 'omg');
if($something['a']=='bla'){
echo "|4| Yeah!! 'bla' found in array ||";
}
/**
* OUTPUT:
* |1| The 'omg' element value found in the assoc array ||
* |2| The 'omg' element value found in the index array ||
* |3| The 'bla' element value found in the assoc array ||
* |4| Yeah!! 'bla' found in array ||
*/
?>
Qui è PHP DEMO
array_key_exists()controlla le chiavi dell'array mentre quest'ultimo $search_arraycontiene l'array associativo. Senza dubbio non funzionerà. Dovresti array_flip()prima.
Puoi usare:
array_search()in_array()array_flip()earray_key_exists()Per verificare se l'indice è definito: isset($something['say'])
Puoi verificare se un array ha o meno un certo elemento con isset () o talvolta anche meglio con array_key_exists () (la documentazione spiega le differenze). Se non puoi essere sicuro che l'array abbia un elemento con l'indice 'say' dovresti prima testarlo o potresti ricevere messaggi 'warning: undefined index ....'.
Per quanto riguarda il test se il valore dell'elemento è uguale a una stringa, puoi usare == o (di nuovo a volte meglio) l'operatore di identità === che non consente la giocoleria di tipo .
if( isset($something['say']) && 'bla'===$something['say'] ) {
// ...
}
in_array () va bene se stai solo controllando, ma se hai bisogno di controllare che esista un valore e restituire la chiave associata, array_search è un'opzione migliore.
$data = [
'hello',
'world'
];
$key = array_search('world', $data);
if ($key) {
echo 'Key is ' . $key;
} else {
echo 'Key not found';
}
Verrà stampato "Key is 1"
Usa semplicemente la funzione PHP array_key_exists()
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
<?php
if (in_array('your_variable', $Your_array)) {
$redImg = 'true code here';
} else {
$redImg = 'false code here';
}
?>
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Un altro uso di in_array in_array () con un array come ago
<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array('p', 'h'), $a)) {
echo "'ph' was found\n";
}
if (in_array(array('f', 'i'), $a)) {
echo "'fi' was found\n";
}
if (in_array('o', $a)) {
echo "'o' was found\n";
}
?>
Supponendo che tu stia usando un semplice array
. vale a dire
$MyArray = array("red","blue","green");
Puoi usare questa funzione
function val_in_arr($val,$arr){
foreach($arr as $arr_val){
if($arr_val == $val){
return true;
}
}
return false;
}
Uso:
val_in_arr("red",$MyArray); //returns true
val_in_arr("brown",$MyArray); //returns false