What is the function to search for a specific value in an array and return its key in PHP?
To search for a specific value in an array and return its key in PHP, you can use the array_search() function. This function searches an array for a given value and returns the corresponding key if the value is found. If the value is not found, it returns false. Here is an example of how to use array_search() to search for a specific value in an array and return its key:
$fruits = array('apple', 'banana', 'orange', 'grape');
$search_value = 'orange';
$key = array_search($search_value, $fruits);
if($key !== false) {
echo "The key of '$search_value' is: $key";
} else {
echo "Value not found in the array";
}
Keywords
Related Questions
- What are the potential pitfalls of using SELECT * in SQL queries, as advised in the forum responses?
- How can brute-force methods be avoided when calculating date differences in PHP, especially when dealing with weekends?
- How can global variables be properly set and utilized within PHP functions, such as in the check_rechte() function?