Current Location: Home> Latest Articles> Use array_combine and in_array() to build a lookup table

Use array_combine and in_array() to build a lookup table

M66 2025-05-16

In PHP, array operations are very common. For some data that requires frequent searches, using an efficient lookup table can greatly improve the performance of the program. array_combine and in_array() are two very useful functions that can help us build lookup tables efficiently and do quick searches. This article will explore how to use these two functions to achieve efficient search.

1. Overview of array_combine() function

array_combine() is a function used to create a new array. It accepts two arrays, the first array as key and the second array as value. It corresponds the elements of these two arrays to generate an array of key-value pairs.

Function prototype:

 array_combine(array $keys, array $values): array

If the two arrays passed in are not equal in length, array_combine() will return false . This function is very suitable for building lookup tables, because we can use the keys of the array as query items and the value of the array as query results.

2. Overview of the in_array() function

in_array() is a function used to check whether a value exists in an array. Return true if the value exists in the array, otherwise false .

Function prototype:

 in_array(mixed $needle, array $haystack, bool $strict = false): bool

This function can be used very conveniently for search operations, especially when we need to check whether an element exists in the built lookup table.

3. Build a lookup table through array_combine and in_array()

Suppose we have a set of user IDs and corresponding user names, and we hope to quickly find the corresponding username through the user ID. We can use array_combine() to create a lookup table, and then use in_array() to check whether an ID exists in the lookup table.

Sample code:

 <?php
// user ID Array
$user_ids = [101, 102, 103, 104, 105];

// user名Array
$usernames = ["alice", "bob", "charlie", "david", "eve"];

// use array_combine Create a lookup table
$user_lookup = array_combine($user_ids, $usernames);

// 检查某个user ID Does it exist
$search_id = 103;
if (in_array($search_id, array_keys($user_lookup))) {
    echo "user ID $search_id exist,user名是:" . $user_lookup[$search_id];
} else {
    echo "user ID $search_id 不exist。";
}
?>

In the above code, we combine the user ID array and the user name array into a lookup table $user_lookup using array_combine() , and then use in_array() and array_keys() to determine whether a user ID exists. array_keys($user_lookup) gets all keys in the lookup table (i.e. user ID), and then use in_array() to determine whether the ID we are looking for is included.

4. Use URL replacement function

If your lookup table involves a URL and the URL contains a domain name, you can use str_replace() or other string processing functions to replace the domain name with the specified value. For example, if you have an array containing URLs, you can replace the domain name with m66.net .

Sample code:

 <?php
// original URL Array
$urls = [
    "https://example.com/page1",
    "https://example.com/page2",
    "https://example.com/page3"
];

// use array_map and str_replace Replace domain name
$modified_urls = array_map(function($url) {
    return str_replace("example.com", "m66.net", $url);
}, $urls);

// The output is replaced URL
print_r($modified_urls);
?>

In this example, we use array_map() to process each URL in the array, and use str_replace() to replace all example.com domain names with m66.net .

5. Performance optimization and summary

The advantage of building a lookup table using array_combine() and in_array() is that the time complexity of the lookup operation is O(1), which means that the search efficiency is very high regardless of the amount of data. In actual development, it is very common to build a lookup table and query it, especially when it is necessary to frequently search for certain data.

When dealing with URL replacement, it is very convenient to use array_map() and str_replace() to perform batch replacements, which is very useful for scenarios where a large number of URLs need to be modified.

Through the above methods, we can not only efficiently build lookup tables, but also quickly find and replace data when needed.