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 OverflowPHP
php.net โบ manual โบ en โบ function.in-array.php
PHP: in_array - Manual
Returns true if needle is found in the array, false otherwise. ... <?php $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?>
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 Code Golf: Detect if string contains an element from array.
There has been many use cases in the past so in some cases using a function that is case sensitive would have been the best route. But as far as this post goes, Iโm just interested to see how the PHP community approach this. ... This will work only if you can depend on your array values having ... More on reddit.com
How to check if Array Contains a Specific Word in PHP
Well, if @return boolean, then it shouldn't return NULL on a non-existence. Here is an example that some people may see as "less overviewable", but maybe "fancier". Didn't test speed. I probally should have added a DocBlock to it, so it's a bit more understandable. It's filtering the array for the stripos !== FALSE output and then checks if the array then is empty or not. This could be worse when a big array is checked. The foreach would return true immediatly. Url: http://sandbox.onlinephpfunctions.com/code/663313f19f85747f83113d1ab10665cd8000d1a4 P.S. If you didn't understand my first sentence: add a return false after the foreach ;) More on reddit.com
Videos
04:30
PHP Array Search Functions: in_array(), array_search(), ...
14:15
15: How to check if an element is in a PHP array - PHP 7 Tutorial ...
04:31
How to check Multiple value exists in an Array in PHP - YouTube
How to find if a value exists in an array in PHP
00:59
The in_array() function in Php. #php #php_tutorial #coding #shorts ...
15:09
How To Work With Arrays In PHP - Full PHP 8 Tutorial - YouTube
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
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
Reddit
reddit.com โบ r/php โบ php code golf: detect if string contains an element from array.
r/PHP on Reddit: PHP Code Golf: Detect if string contains an element from array.
November 17, 2017 - There has been many use cases in the past so in some cases using a function that is case sensitive would have been the best route. But as far as this post goes, Iโm just interested to see how the PHP community approach this. ... This will work only if you can depend on your array values having no regex characters.
W3Schools
w3schools.com โบ php โบ php_string.asp
PHP Strings
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
ReqBin
reqbin.com โบ code โบ php โบ prd64jee โบ php-in-array-example
How to check if an element exists in a PHP array?
July 27, 2023 - To check if an element exists in a PHP array, you can use the in_array($search, $array, $mode) function. The $search parameter specifies the element or value to search in the specified array and can be of a mixed type (string, integer, or other ...
Laravel
laravel.com โบ docs โบ 12.x โบ artisan
Artisan Console | Laravel 12.x - The clean stack for Artisans and agents
Tinker utilizes an "allow" list to determine which Artisan commands are allowed to be run within its shell. By default, you may run the clear-compiled, down, env, inspire, migrate, migrate:install, up, and optimize commands. If you would like to allow more commands you may add them to the commands array in your tinker.php configuration file:
GeeksforGeeks
geeksforgeeks.org โบ dsa โบ find-second-largest-element-array
Second Largest Element in an Array - GeeksforGeeks
Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.
Published ย February 10, 2025
W3Schools
w3schools.com โบ python โบ python_arrays.asp
Python Arrays
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Laravel
laravel.com โบ docs โบ 12.x โบ eloquent
Eloquent: Getting Started | Laravel 12.x - The clean stack for Artisans and agents
As we have seen, Eloquent methods like all and get retrieve multiple records from the database. However, these methods don't return a plain PHP array.
W3Schools
w3schools.com โบ php โบ php_arrays_add.asp
PHP Add Array Items
Arrays Indexed Arrays Associative Arrays Create Arrays Access Array Items Update Array Items Add Array Items Remove Array Items Sorting Arrays Multidimensional Arrays Array Functions PHP Superglobals
GeeksforGeeks
geeksforgeeks.org โบ php โบ php-in_array-function
PHP in_array() Function - GeeksforGeeks
June 2, 2025 - The PHP in_array() function is a straightforward and handy tool for checking if a value exists within an array. Its flexibility with the optional strict type checking and compatibility with various data types make it a staple in PHP programming.
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Array โบ includes
Array.prototype.includes() - JavaScript | MDN
The includes() method of Array instances determines whether an array includes a certain value among its entries, returning true or false as appropriate.