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.

php Courses
php Courses
php Courses
How to Use the PHP array_search() Function

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: array_search($value, $array, $strict = false) 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

$fruits = array('apple', 'banana', 'cherry', 'orange');
$key = array_search('cherry', $fruits);
echo $key; // outputs 2

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.

$numbers = array(10, '20', 30, '40');
$key = array_search('20', $numbers, true);
echo $key; // outputs nothing (null)

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():

$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

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.

$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

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)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPstrict modemultidimensional arrayarray_search
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.