• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Php Tip: Use array_search() to search an array for a value and return the key

December 19, 2023

Php Tip: Use array_search() to search an array for a value and return the key

When working with arrays in PHP, it is often necessary to search for a specific value and retrieve its corresponding key. The array_search() function in PHP provides a convenient way to accomplish this task. In this article, we will explore how to use array_search() effectively to search an array for a value and return the key.

Understanding array_search()

The array_search() function in PHP searches an array for a given value and returns the corresponding key if found. Its syntax is as follows:

array_search($value, $array, $strict)

Here, $value represents the value to search for, $array is the array to search in, and $strict is an optional parameter that determines whether the search should be performed strictly (both value and type must match) or not. By default, $strict is set to false.

Using array_search() to search an array

Let’s consider an example where we have an array of fruits:

$fruits = array("apple", "banana", "orange", "grape");

To search for the key of a specific fruit, such as “orange,” we can use the array_search() function as follows:

$key = array_search("orange", $fruits);

If the value is found in the array, the array_search() function will return the corresponding key. In this case, the value of $key will be 2, as “orange” is at the index 2 in the array.

If the value is not found in the array, the array_search() function will return false. Therefore, it is important to check the return value using the === operator to ensure an accurate result.

Using strict mode with array_search()

By default, the array_search() function performs a loose comparison, meaning it matches the value regardless of its type. However, if you want to perform a strict comparison, you can set the $strict parameter to true.

For example, let’s consider an array with both string and integer values:

$data = array("1", 2, "3", 4);

If we search for the value 2 without strict mode, it will be found at index 1:

$key = array_search(2, $data);

However, if we enable strict mode, the search will fail because the value 2 is an integer, while the array contains it as a string:

$key = array_search(2, $data, true);

In this case, the array_search() function will return false as the value is not found.

Conclusion

The array_search() function in PHP provides a convenient way to search for a value in an array and retrieve its corresponding key. By understanding its usage and optional parameters, you can effectively utilize this function in your PHP projects. Remember to check the return value using the === operator to ensure accurate results.

For more information on PHP and VPS hosting solutions, visit Server.HK.

Recent Posts

  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot