How do you get the key name of a specific value in an array using PHP?
In PHP, you can use the array_search() function to retrieve the key of a specified value in an array. Here is an example:
$fruits = array("apple", "banana", "orange", "grape");
$key = array_search("orange", $fruits);
if($key !== false) {
echo "The key for 'orange' is: " . $key;
} else {
echo "'orange' is not found in the array";
}
In this example, we first define an array $fruits containing fruit names. Then we use the array_search() function to locate the element in the array with the value “orange” and store its key in the variable $key. Finally, we check if $key is false (meaning the value was not found), if it is found, we print out the key name for “orange”.