September 28, 2025
Get all keys as an array in PHP of a given array
We can get all the keys of a given array as a new array by using PHP’s built-in function array_keys(). This function takes an array as its input parameter and returns all of its keys in the form of a new array. If the input array is a simple indexed or associative array, it will return the corresponding indexes or string keys. However, if the input is a multidimensional array, array_keys() will only return the keys of the outer or main array and will not automatically go deeper into the nested arrays.
Sample Code
$data = ["id" => 1, "name" => "Marcus", "age" => 30];
$keys = array_keys($data);
print_r($keys);
Output
Array
(
[0] => id
[1] => name
[2] => age
)