Array search functions help you find if a value exists in a list and where it is. This saves time compared to checking each item yourself.
Array search functions in PHP
<?php // Search for a value in an array and get its key $key = array_search(mixed $needle, array $haystack, bool $strict = false); // Check if a value exists in an array $exists = in_array(mixed $needle, array $haystack, bool $strict = false); ?>
array_search returns the key of the found value or false if not found.
in_array returns true if the value exists, otherwise false.
<?php $fruits = ['apple', 'banana', 'cherry']; $key = array_search('banana', $fruits); echo $key; // Outputs 1 ?>
<?php $fruits = ['apple', 'banana', 'cherry']; $exists = in_array('grape', $fruits); echo $exists ? 'Found' : 'Not found'; // Outputs 'Not found' ?>
<?php $emptyArray = []; $key = array_search('anything', $emptyArray); var_dump($key); // Outputs bool(false) ?>
<?php $singleElement = ['only']; $key = array_search('only', $singleElement); echo $key; // Outputs 0 ?>
This program shows how to use array_search and in_array to find values in arrays. It prints the array, searches for values, and handles empty arrays.
<?php // Define an array of colors $colors = ['red', 'green', 'blue', 'yellow']; // Print the original array echo "Original array:\n"; print_r($colors); // Search for 'blue' in the array $searchKey = array_search('blue', $colors); if ($searchKey !== false) { echo "\nFound 'blue' at key: $searchKey\n"; } else { echo "\n'blue' not found in the array.\n"; } // Check if 'purple' exists in the array $exists = in_array('purple', $colors); echo $exists ? "\n'purple' is in the array.\n" : "\n'purple' is not in the array.\n"; // Search for 'red' in an empty array $emptyArray = []; $emptySearch = array_search('red', $emptyArray); echo $emptySearch === false ? "\n'red' not found in empty array.\n" : "\nFound 'red' in empty array.\n"; ?>
Time complexity: Both array_search and in_array check items one by one, so they take time proportional to the array size (O(n)).
Space complexity: These functions do not use extra space beyond a few variables.
A common mistake is to check if (array_search(...) == false) which fails if the found key is 0. Always use strict comparison !== false.
Use array_search when you need the key of the found value. Use in_array when you only need to know if the value exists.
Array search functions help find values or keys in arrays easily.
array_search returns the key or false if not found.
in_array returns true or false depending on presence.
Practice
false if not found?Solution
Step 1: Understand function purpose
array_searchlooks for a value and returns its key if found.Step 2: Compare with other functions
in_arrayreturns true/false, not key;array_key_existschecks keys;array_keysreturns all keys.Final Answer:
array_search -> Option BQuick Check:
Key returned by array_search = array_search [OK]
- Confusing array_search with in_array
- Thinking array_key_exists returns value
- Assuming array_keys searches values
$arr using in_array?Solution
Step 1: Recall in_array syntax
The correct syntax isin_array(value, array).Step 2: Match syntax with options
in_array(5, $arr); matchesin_array(5, $arr);which is correct.Final Answer:
in_array(5, $arr); -> Option AQuick Check:
in_array(value, array) = in_array(5, $arr) [OK]
- Swapping parameters order
- Using arrow syntax instead of comma
- Passing array as first argument
$arr = ['apple', 'banana', 'cherry'];
$key = array_search('banana', $arr);
echo $key;Solution
Step 1: Understand array_search behavior
It returns the key of the searched value if found.Step 2: Check array keys and searched value
Array keys are 0=>'apple', 1=>'banana', 2=>'cherry'. Searching 'banana' returns key 1.Final Answer:
1 -> Option DQuick Check:
array_search('banana', $arr) = 1 [OK]
- Expecting value instead of key
- Confusing false with 0
- Assuming it returns boolean
$arr = ['x' => 10, 'y' => 20];
if (array_search(30, $arr)) {
echo 'Found';
} else {
echo 'Not Found';
}Solution
Step 1: Analyze array_search return and condition
array_search returns key or false. If key is 0, condition fails because 0 is falsey.Step 2: Check searched value presence
30 is not in array, so array_search returns false, condition is false, prints 'Not Found'. But if value was at key 0, condition would fail incorrectly.Step 3: Suggest correct function for boolean check
Usein_arraywhich returns true/false directly for presence check.Final Answer:
array_search should be replaced with in_array for this check -> Option CQuick Check:
Use in_array for boolean presence check [OK]
- Using array_search in if without strict check
- Ignoring 0 key treated as false
- Not using === to check false
$data = ['a' => 0, 'b' => false, 'c' => null, 'd' => '0'];, which function call correctly checks if the value 0 exists in the array considering type?Solution
Step 1: Understand strict checking
Using third parameter true enforces type checking in in_array and array_search.Step 2: Analyze array values and types
Values are 0 (int), false (bool), null, and '0' (string). Only strict check distinguishes 0 (int) from others.Step 3: Choose correct function and parameters
in_array(0, $data, true) returns true only if exact int 0 found. array_search without strict may confuse types.Final Answer:
in_array(0, $data, true); -> Option AQuick Check:
Use in_array with strict=true for exact type match [OK]
- Ignoring strict parameter causing false positives
- Using array_search without strict for type-sensitive check
- Confusing '0' string with 0 integer
Ready to prove it?
Pass the test and this pattern moves unknown
