Content · Programming · PHP

PHP array_search() Function: Usage and Examples

Understand array_search() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to find the first key of a matching value in an array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The array_search() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is array_search() in PHP?

Find the first key of a matching value in an array.

Its syntax is:

array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false

Parameters used by this operation:

  • $needle — The value supplied for this argument affects the operation.
  • $haystack — The collection being examined or updated.
  • $strict — When true, comparisons check both value and type.

Test the return value with === false: key 0 is a valid result. Prefer strict=true to avoid type coercion.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$departments = ["Sales", "Support", "Finance"];
var_dump(array_search("Support", $departments, true));

Output:

int(1)

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$codes = [0, 10, 20];
var_dump(array_search("0", $codes));
var_dump(array_search("0", $codes, true));

Output:

int(0)
bool(false)

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Look up a product identifier by a known exact product code

Look up a product identifier by a known exact product code. Save the code in a PHP file and adapt the sample data to your project.

<?php

$productCodes = ["notebook" => "NB-101", "folder" => "FD-204"];
$productKey = array_search("FD-204", $productCodes, true);
if ($productKey !== false) { echo "Found product: " . $productKey; }

Output:

Found product: folder

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

Test the return value with === false: key 0 is a valid result. Prefer strict=true to avoid type coercion.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
array_search()Find the first key of a matching value in an array
Related functionsin_array() tests presence; array_keys() can return all matching keys.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming input and result have identical keysCheck whether the function reindexes keys or retains original associations.
Confusing mutation with a returned copyTest the return value with === false: key 0 is a valid result. Prefer strict=true to avoid type coercion.
Choosing a related function with different behaviorin_array() tests presence; array_keys() can return all matching keys.

Key Takeaways

  • array_search() helps you find the first key of a matching value in an array.
  • Test the return value with === false: key 0 is a valid result. Prefer strict=true to avoid type coercion.
  • in_array() tests presence; array_keys() can return all matching keys.
  • Use the practical example as a starting point, but validate real application inputs.

array_search() is useful when you need to find the first key of a matching value in an array. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.