- PHP

How to get array key name in PHP

To get the array key name with its value, use the array_search() function.

array_search('value', $array);

Example:

<?php

$myArray = [
    'name' => 'Kate',
    'pet' => 'Dog', 
    'hobby' => 'Basketball', 
];

$key = array_search('Dog', $myArray);
echo $key;

?>

Output: pet