Current Location: Home> Latest Articles> Use array_search() to quickly locate the key using value to make keys

Use array_search() to quickly locate the key using value to make keys

M66 2025-05-18

How to use array_flip() and array_search() to achieve fast positioning from value to key?

In PHP, we often need to do quick searches in arrays. Sometimes we need to find the corresponding key through the value, or look up the key in reverse from a value. PHP provides two very useful functions: array_flip() and array_search() , which can help us achieve quick positioning from values ​​to keys. This article will introduce how to use these two functions to accomplish this task.

1. Introduction to array_flip() function

The array_flip() function will exchange keys and values ​​in the array, making the original value become the key of the array, and the original key becomes the value. Suppose you have an associative array that you can invert through array_flip() so that the value can be found as a new key.

 <?php
$array = [
    'apple' => 1,
    'banana' => 2,
    'orange' => 3
];

$flipped = array_flip($array);
print_r($flipped);
?>

Output result:

 Array
(
    [1] => apple
    [2] => banana
    [3] => orange
)

In this example, array_flip() turns the value of the original array into a key, and the original key into a value.

2. Introduction to array_search() function

The array_search() function can be used to find a given value in an array and return its corresponding key. If a matching value is found, array_search() returns the key of the value; if no matching value is found, false is returned.

 <?php
$array = [
    'apple' => 1,
    'banana' => 2,
    'orange' => 3
];

$key = array_search(2, $array);
echo "The key for value 2 is: " . $key;
?>

Output result:

 The key for value 2 is: banana

3. Use array_flip() and array_search() to combine

We can quickly locate the key corresponding to a value by first using the array_flip() function to swap the values ​​and keys of the array, and then use array_search() .

 <?php
$array = [
    'apple' => 1,
    'banana' => 2,
    'orange' => 3
];

// Invert the array first
$flipped = array_flip($array);

// use array_search() Find the key corresponding to the value
$key = array_search(2, $flipped);
echo "The original key for value 2 is: " . $key;
?>

Output result:

 The original key for value 2 is: banana

4. Why is this effective?

This method is more efficient because array_flip() stores all values ​​as keys, so you can quickly locate by directly looking up the keys. In contrast, if you use array_search() directly to find the keys of the value in the original array, each search requires iterating through the entire array. By inverting the array, the search process becomes very efficient.

5. Practical application scenarios

This method is suitable for scenarios where we need to frequently search from values ​​to keys for quick searches. For example, if you have an array of user information that contains the user name and the corresponding user ID, and you often get the user name based on the user ID, using the combination of array_flip() and array_search() can greatly improve efficiency.

 <?php
$userIDs = [
    'John' => 1001,
    'Jane' => 1002,
    'Tom' => 1003
];

// Will ID As a key
$flippedUserIDs = array_flip($userIDs);

// Find usersIDfor1002Username
$username = array_search(1002, $flippedUserIDs);
echo "The username for userID 1002 is: " . $username;
?>

Output result:

 The username for userID 1002 is: Jane

6. Things to note

  • array_flip() can only handle arrays where values ​​are unique. If there are duplicates in the value in the array, array_flip() will only retain the last value as the key, and the previous value will be overwritten.

  • When array_search() cannot find the value, it will return false , so you need to pay attention to checking the return value when using it.

Hopefully this article helps you better understand how to use array_flip() and array_search() to quickly locate from value to key. If you have more questions or need further help, feel free to ask questions!