Hoe kan ik controleren of $something['say']
de waarde 'bla'
of 'omg'
heeft?
$something = array('say' => 'bla', 'say' => 'omg');
Antwoord 1, autoriteit 100%
Gebruik je de instructie if
?
if(isset($something['say']) && $something['say'] === 'bla') {
// do something
}
Trouwens, je wijst twee keer een waarde toe met de toets say
, dus je array zal resulteren in een array met slechts één waarde.
Antwoord 2, autoriteit 91%
Je zou de PHP in_arrayfunctie
kunnen gebruiken
if( in_array( "bla" ,$yourarray ) )
{
echo "has bla";
}
Antwoord 3, autoriteit 43%
Gebruik: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";
}
Hier is uitvoer:The 'prize_id' element is in the array
Gebruik: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";
}
Geen uitvoer
Concluderend, array_key_exists()
werkt niet met een eenvoudige array. Het is alleen om te achterhalen of een arraysleutel bestaat of niet. Gebruik in plaats daarvan in_array()
.
Hier is meer voorbeeld:
<?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 ||
*/
?>
Hier is PHP DEMO
Antwoord 4, autoriteit 8%
U kunt gebruiken:
array_search()
in_array()
- een combinatie van
array_flip()
enarray_key_exists()
Antwoord 5, autoriteit 5%
Om te controleren of de index is gedefinieerd: isset($something['say'])
Antwoord 6, autoriteit 4%
Je kunt testen of een array een bepaald element bevat of niet met isset()of soms nog beter array_key_exists()(de documentatie legt de verschillen uit). Als u niet zeker weet of de array een element heeft met de index ‘say’, moet u dat eerst testen, anders krijgt u mogelijk ‘warning: undefined index….’-berichten.
Voor de test of de waarde van het element gelijk is aan een string kun je == gebruiken of (alweer soms beter) de identiteitsoperator ===waarbij type jongleren niet is toegestaan .
if( isset($something['say']) && 'bla'===$something['say'] ) {
// ...
}
Antwoord 7, Autoriteit 4%
In_array () is prima als u alleen aan het controleren bent, maar als u moet controleren of er een waarde bestaat en de bijbehorende toets retourneert, is array_search een betere optie.
$data = [
'hello',
'world'
];
$key = array_search('world', $data);
if ($key) {
echo 'Key is ' . $key;
} else {
echo 'Key not found';
}
Dit wordt afdruk ‘sleutel is 1 “
Antwoord 8, Autoriteit 2%
Gebruik gewoon de PHP-functie 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";
}
?>
Antwoord 9, Autoriteit 2%
<?php
if (in_array('your_variable', $Your_array)) {
$redImg = 'true code here';
} else {
$redImg = 'false code here';
}
?>
Antwoord 10
Nou, eerst een associatieve array kan alleen een sleutel hebben gedefinieerd, dus deze array zou nooit bestaan. Gebruik anders in_array()
om te bepalen of dat specifieke arrayelement in een scala aan mogelijke oplossingen is.
Antwoord 11
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
een ander gebruik van in_array
in_array () met een array als naald
<?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";
}
?>
Antwoord 12
Ervan uitgaande dat u een eenvoudige array gebruikt
. d.w.z.
$MyArray = array("red","blue","green");
U kunt deze functie gebruiken
function val_in_arr($val,$arr){
foreach($arr as $arr_val){
if($arr_val == $val){
return true;
}
}
return false;
}
Gebruik:
val_in_arr("red",$MyArray); //returns true
val_in_arr("brown",$MyArray); //returns false