How to Use the PHP array_search() Function
This tutorial explains the PHP array_search() function, covering its basic syntax, optional strict mode, searching within multidimensional arrays, and using an offset to find values after a specific position, complete with clear code examples for each scenario.
In PHP, arrays are a versatile data type that can store multiple values and be accessed via indexes. The array_search() function allows you to search for a given value within an array and returns the corresponding key.
Basic usage
The syntax of array_search() is:
<code>array_search($value, $array, $strict = false)</code>where $value is the value to search for, $array is the array to search in, and $strict (optional) determines whether the search should also compare types (default is false ).
Simple example
<code>$fruits = array('apple', 'banana', 'cherry', 'orange');
$key = array_search('cherry', $fruits);
echo $key; // outputs 2</code>This example defines an array $fruits and uses array_search() to find the element "cherry", which returns the key 2 .
Using strict mode
By default, array_search() does not compare types. Setting the $strict parameter to true enables strict comparison of both type and value.
<code>$numbers = array(10, '20', 30, '40');
$key = array_search('20', $numbers, true);
echo $key; // outputs nothing (null)</code>Because the string "20" is not identical to the integer 20 , the function returns null when strict mode is enabled.
Searching in multidimensional arrays
array_search() only searches the first dimension of an array. To search for a value in a specific column of a multidimensional array, combine it with array_column() :
<code>$students = array(
array('name' => 'Alice', 'age' => 20),
array('name' => 'Bob', 'age' => 25),
array('name' => 'Charlie','age' => 30),
array('name' => 'David', 'age' => 35)
);
$key = array_search(25, array_column($students, 'age'));
echo $key; // outputs 1</code>This retrieves the index of the student whose age is 25 , which is 1 .
Searching after a specific offset
The third parameter of array_search() can be used as an offset to start the search from a given position.
<code>$numbers = array(10, 20, 30, 40, 50, 60);
$key = array_search(40, $numbers, true);
$offset = $key + 1;
$next_key = array_search(50, $numbers, false, $offset);
echo $next_key; // outputs 4</code>Here, after finding the key of value 40 , the search for 50 begins from the next position, returning the key 4 .
Summary
The article covered the usage of the PHP array_search() function, including its basic syntax, strict mode searching, searching within multidimensional arrays, and using an offset to locate values after a specific position. Mastering these techniques is essential for PHP developers.
Recommended PHP Learning Resources
Vue3+Laravel8+Uniapp Beginner to Advanced Development Tutorial
Vue3+TP6+API Social E‑commerce System Development Course
Swoole From Beginner to Master Course
Workerman+TP6 Real‑time Chat System (Limited Time Offer)
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.