How to usethe array_keys() Function in PHP
The array_keys() function returns an array containing the keys of an associative array.
<?php
$subjects = array(
"Maths" => 95,
"Physics" => 90,
"Chemistry" => 96,
"English" => 93,
"Computer" => 98
);
$keys = array_keys($subjects);
print_r($keys);
?>
Output
Array ( [0] => Maths [1] => Physics [2] => Chemistry [3] => English [4] => Computer )
How to Get All Keys of an Associative Array in PHP?
Given an Associative array, the task is to get all the keys of the associative array in PHP. There are two approaches to get all keys from the associative array, these are – using array_keys(), and foreach loop. In this article, we will explore these approaches with examples.
Table of Content
- Using the array_keys() Function
- Using a foreach Loop