Horje
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.

Using the array_keys() Function

The array_keys() function returns an array containing the keys of an associative array.

PHP
<?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
)

Using a foreach Loop

You can use a foreach loop to iterate over the associative array and extract the keys of array.

PHP
<?php

$subjects = array(
    "Maths" => 95, 
    "Physics" => 90,   
    "Chemistry" => 96, 
    "English" => 93,   
    "Computer" => 98
);

$keys = [];

foreach ($subjects as $key => $value) {
    $keys[] = $key;
}

print_r($keys);

?>

Output
Array
(
    [0] => Maths
    [1] => Physics
    [2] => Chemistry
    [3] => English
    [4] => Computer
)

Using the array_walk() Function

The 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.

PHP
<?php

$subjects = array(
    "Maths" => 95, 
    "Physics" => 90,   
    "Chemistry" => 96, 
    "English" => 93,   
    "Computer" => 98
);

$keys = [];

array_walk($subjects, function($value, $key) use (&$keys) {
    $keys[] = $key;
});

print_r($keys);

?>

Output
Array
(
    [0] => Maths
    [1] => Physics
    [2] => Chemistry
    [3] => English
    [4] => Computer
)

Using array_map() Function

The 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

PHP
<?php
function getArrayKeysUsingMap($array) {
    return array_map(function($key) {
        return $key;
    }, array_keys($array));
}

$associativeArray = [
    "name" => "John",
    "age" => 30,
    "city" => "New York"
];

$keys = getArrayKeysUsingMap($associativeArray);

echo "Keys of the associative array are:\n";
print_r($keys);
?>

Output
Keys of the associative array are:
Array
(
    [0] => name
    [1] => age
    [2] => city
)



Reffered: https://www.geeksforgeeks.org


PHP

Related
How to Check Whether a Variable is Empty in PHP? How to Check Whether a Variable is Empty in PHP?
How to Replace Part of a string with Another String in PHP? How to Replace Part of a string with Another String in PHP?
How to Add Elements to the End of an Array in PHP? How to Add Elements to the End of an Array in PHP?
How to Get Number of Days in Current Month in PHP? How to Get Number of Days in Current Month in PHP?
PHP Program to Add Two Polynomials PHP Program to Add Two Polynomials

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16