![]() |
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() FunctionThe array_keys() function returns an array containing the keys of an associative array.
Output Array ( [0] => Maths [1] => Physics [2] => Chemistry [3] => English [4] => Computer ) Using a foreach LoopYou can use a foreach loop to iterate over the associative array and extract the keys of array.
Output Array ( [0] => Maths [1] => Physics [2] => Chemistry [3] => English [4] => Computer ) Using the array_walk() FunctionThe array_walk() function can be used to apply a callback function to each element of the array. We can use this to collect all the keys.
Output Array ( [0] => Maths [1] => Physics [2] => Chemistry [3] => English [4] => Computer ) Using array_map() FunctionThe array_map() function can be used to apply a callback function to the elements of an array. We can use this function to extract all the keys from an associative array by mapping the keys to a new array. Example
Output Keys of the associative array are: Array ( [0] => name [1] => age [2] => city ) |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |