Web Design Brisbane

What is array_search in PHP?

array_search() is a built-in function in PHP that searches an array for a given value and returns the key of the first element that matches the value. If the value is not found in the array, it returns FALSE.

The syntax for array_search() is as follows:

array_search(value, array, strict);

  • value is the value that you want to search for in the array
  • array is the input array that you want to search in
  • strict is an optional parameter that indicates whether the search should be done in strict mode (type and value must be equal) or not. Default is false
For example, to search for the key of the value "cherry" in an array:

$fruits = array("apple", "banana", "cherry");
$key = array_search("cherry", $fruits);
echo $key; // Output: 2

In the example above, the function searches the array for the value "cherry" and returns the key of the first element that matches it, which is 2.

array_search() function is useful when you need to find the key of a specific value in an array, for example, when you want to delete or update a specific element in an array, or when you want to check if a specific value exists in an array.

Please note that the array_search() function performs a linear search and has a time complexity of O(n) where n is the number of elements in the array.

Also, it is important to keep in mind that array_search() function only returns the first key that matches the value, if there are multiple occurrences of the value in the array, it will return the key for the first one.

array_search() function in PHP is used to search an array for a specific value and return the key of the first element that matches the value. Some common use cases of array_search() function are:

  • Finding the key of a specific value in an array: This can be useful when you want to delete or update a specific element in an array, or when you want to check if a specific value exists in an array.

$fruits = array("apple", "banana", "cherry");
$key = array_search("cherry", $fruits);
echo $key; // Output: 2

Creating a lookup table: you can use array_search() function to create a lookup table where you can find a value by its key.

$lookup = array_flip($fruits);
echo $lookup['cherry']; // Output: 2

Matching values in arrays: array_search() function can be used to find values in multiple arrays and check if they match.

$a1 = array("a" => "green", "red", "blue");
$a2 = array("b" => "green", "yellow", "red");
$result = array_search('green', $a1);
if ($result !== false) {
echo "Value found in array 1 at key: $result";
} else {
$result = array_search('green', $a2);
echo "Value found in array 2 at key: $result";
}

  • Searching for specific value in large arrays: array_search() function is useful when you have large arrays and you need to find a specific value in them, it performs a linear search and has a time complexity of O(n) where n is the number of elements in the array.

Please note that array_search() only returns the first key that matches the value. If there are multiple occurrences of the value in the array, it will return the key for the first one.

Do you need support in your PHP web development project in Brisbane, Sydney, Melbourne, Gold Coast

Get CMTech Brisbane-based PHP development company, to help you with your web development project. We are based in Brisbane and have supporting clients across Australia since 2010. Call us on 07 3272 7155 or fill out the enquiry form to discuss your PHP web development.

Bottom Tags : Web Design Brisbane, Web Development Brisbane, web design company brisbane, Custom Software Development Brisbane