How to check if an array value exists?


Question

How can I check if $something['say'] has the value of 'bla' or 'omg'?

$something = array('say' => 'bla', 'say' => 'omg');
1
96
2/12/2014 8:22:29 PM

Accepted Answer

Using if?

if(isset($something['say']) && $something['say'] == 'bla') {
    // do something
}

Btw, you are assigning an value with the key say twice, hence your array will result in an array with only one value.

97
3/13/2010 11:34:31 PM

You could use the PHP in_array function

if( in_array( "bla" ,$yourarray ) )
{
    echo "has bla";
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon