How can you retrieve the key of an array when you know the value in PHP?
To retrieve the key of an array when you know the value in PHP, you can use the array_search() function. This function searches an array for a given value and returns the corresponding key if found. You can then use this key to access the element in the array.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$value = 2;
$key = array_search($value, $array);
if ($key !== false) {
echo "Key for value $value is: $key";
} else {
echo "Value not found in array";
}