Use the in_array() function.
$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');
if (in_array('kitchen', $array)) {
echo 'this array contains kitchen';
}
Answer from Wiseguy on Stack Overflow Top answer 1 of 8
169
Use the in_array() function.
$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');
if (in_array('kitchen', $array)) {
echo 'this array contains kitchen';
}
2 of 8
24
// Once upon a time there was a farmer
// He had multiple haystacks
$haystackOne = range(1, 10);
$haystackTwo = range(11, 20);
$haystackThree = range(21, 30);
// In one of these haystacks he lost a needle
$needle = rand(1, 30);
// He wanted to know in what haystack his needle was
// And so he programmed...
if (in_array($needle, $haystackOne)) {
echo "The needle is in haystack one";
} elseif (in_array($needle, $haystackTwo)) {
echo "The needle is in haystack two";
} elseif (in_array($needle, $haystackThree)) {
echo "The needle is in haystack three";
}
// The farmer now knew where to find his needle
// And he lived happily ever after
W3Schools
w3schools.com › php › func_array_in_array.asp
PHP in_array() Function
connection_aborted() connection_status() connection_timeout() constant() define() defined() die() eval() exit() get_browser() __halt_compiler() highlight_file() highlight_string() hrtime() ignore_user_abort() pack() php_strip_whitespace() show_source() sleep() sys_getloadavg() time_nanosleep() time_sleep_until() uniqid() unpack() usleep() PHP MySQLi · affected_rows autocommit change_user character_set_name close commit connect connect_errno connect_error data_seek debug dump_debug_info errno error error_list fetch_all fetch_array fetch_assoc fetch_field fetch_field_direct fetch_fields fetch_l
php - Check if string contains a value in array - Stack Overflow
I am trying to detect whether a string contains at least one URL that is stored in an array. ... The string is entered by the user and submitted via PHP. More on stackoverflow.com
php - Check if a specific array element contains a whitelisted value - Stack Overflow
Here is a PHP demo. ... array_key_exists() checks for array keys while the latter $search_array contains associative array. No doubt it won't work. You should array_flip() it first. More on stackoverflow.com
PHP Code Golf: Detect if string contains an element from array.
Throughout my 6+ year career I have had to write code for checking if one or more strings exist in another string. When doing this in the past, I… More on reddit.com
Is there a reason why needle-haystack argument order in builtin PHP functions are inconsistent?
PHP, especially in the early days, was developed in a way where when somebody had a problem they create the patch and pushed it (or well, in CVS, committed it) without design oversight or plan or whatever. In quite a bunch of cases this follows what C does (as many parts of "classic" PHP are directly inspired by C, most extensions are thin wrappers of C libraries etc.) This model worked well to cover lots of ground instead of arguing in committee, which allowed the quick growth PHP had, but lead to some inconsistencies. However, if you look at it in depth it's not totally bad, most cases are similar (with string functions haystack often comes first, with array functions mostly needle first) More on reddit.com
CSS-Tricks
css-tricks.com › snippets › php › php-array-contains
PHP Array Contains | CSS-Tricks
September 13, 2009 - → PHP Array Contains · Chris Coyier on Sep 13, 2009 · Check if value is in array and outputs Yes or No · <?php $names = array( 'Bob', 'Jim', 'Mark' ); echo 'In Array? '; if (in_array(‘foo’, $names)) echo 'Yes'; else echo 'No'; ?> Jimbo · Permalink to comment# September 1, 2010 ·
PHP Tutorial
phptutorial.net › home › php tutorial › php in_array
PHP in_array() Function - PHP Tutorial
April 6, 2025 - The following example uses the in_array() function to find the number 15 in the $user_ids array. It returns true because the in_array() function compares the values using the loose comparison (==): <?php $user_ids = [10, '15', '20', 30]; $result = in_array(15, $user_ids); var_dump($result); // ...
ReqBin
reqbin.com › code › php › prd64jee › php-in-array-example
How to check if an element exists in a PHP array?
If the parameter is set to TRUE, ... is FALSE. In this PHP Search In Array example, we use the in_array() function to check whether the given value exists in the array....
GeeksforGeeks
geeksforgeeks.org › php › php-in_array-function
PHP in_array() Function - GeeksforGeeks
June 2, 2025 - The in_array() function in the PHP works in the following ways: By default, in_array() performs a loose comparison (==) between $needle and the array elements. If $strict is true, it performs a strict comparison (===) which checks both the value and the type. This behavior is important when searching in arrays with mixed types (e.g., strings, integers, floats). Now, let us understand with the help of the example:
O'Reilly
oreilly.com › library › view › php-cookbook › 1565926811 › ch04s12.html
4.11. Checking if an Element Is in an Array - PHP Cookbook [Book]
November 20, 2002 - 4.11. Checking if an Element Is in an ArrayProblem You want to know if an array contains a certain value. SolutionUse in_array( ) : if (in_array($value, $array)) { // an... - Selection from PHP Cookbook [Book]
Authors David SklarAdam Trachtenberg
Published 2002
Pages 640
Top answer 1 of 15
118
Try this.
$string = 'my domain name is website3.com';
foreach ($owned_urls as $url) {
//if (strstr($string, $url)) { // mine version
if (strpos($string, $url) !== FALSE) { // Yoshi version
echo "Match found";
return true;
}
}
echo "Not found!";
return false;
Use stristr() or stripos() if you want to check case-insensitive.
2 of 15
40
If you need it the other way round, i.e. to find if any of array elements contain a specific string, you can do it like this
$array = ["they has mystring in it", "some", "other", "elements"];
if (stripos(json_encode($array),'mystring') !== false) {
echo "found mystring";
}
Though there is a chance for incorrect results:
- a false negative result if your array elements contain characters that json_encode() will escape (e.g if mystring is
http://google.com/) - a false positive result, e.g if mystring is
"some","other". This code will tell you "found" while none of array elements contain such a string.
Hence explicit loop will be more bulletproof (or a brand new PHP function array_find()).
Top answer 1 of 12
369
You could use the PHP in_array function:
if(in_array("bla", $yourarray))
{
echo "has bla";
}
2 of 12
138
Using the instruction if?
if(isset($something['say']) && $something['say'] === 'bla') {
// do something
}
By the way, you are assigning a value with the key say twice, hence your array will result in an array with only one value.
Sling Academy
slingacademy.com › article › how-to-check-if-an-array-contains-a-value-in-php
How to check if an array contains a value in PHP - Sling Academy
This tutorial covers several methods from the simple in_array function to more advanced techniques. The in_array() function is the most straightforward way to check if a value exists in an array. It returns true if the value is found in the array and false otherwise.
Phpmentoring
phpmentoring.org › blog › php-in-array-function
Php in_array() Function | How To Check If A Value Is In An Array PHP
Strict comparison is also known as “===” in PHP. As we pointed out above the in_array() function returns true if the value exists in the array, and false if not. If the value being compared ($needle) is a string, the in_array() function will use case-sensitivity by default when comparing to values in the array ($haystack). In this example the first if statement passes fine, but the second if statement will fail.