To get the numeric index of an associative array, the code is as follows−
Example
Live Demo
Output
This will produce the following output−
Array key and value... key: a, value: 5 key: b, value: 20 key: c, value: 35 key: d, value: 55
Example
Let us now see another example−
Live Demo
Output
This will produce the following output−
Array key and value... key: p, value: 150 key: q, value: 100 key: r, value: 120 key: s, value: 110 Updated Array key and value... key: p, value: 150 key: q, value: 100 key: r, value: 120 key: s, value: 25
Updated on 27-Dec-2019 07:39:23
- Related Questions & Answers
- How to access an associative array by integer index in PHP?
- PHP Associative Array
- Remove duplicated elements of associative array in PHP
- How to generate array key using array index – JavaScript associative array?
- PHP array_push[] to create an associative array?
- How to build dynamic associative array from simple array in php?
- Convert an object to associative array in PHP
- PHP Pushing values into an associative array?
- Associative Arrays in PHP
- PHP program to add item at the beginning of associative array
- How to re-index an array in PHP?
- Return an array with numeric keys PHP?
- How to use associative array/hashing in JavaScript?
- Length of a JavaScript associative array?
- How to get random value out of an array in PHP?
❮ PHP Array Reference
Example
Return an array containing the keys:
Try it Yourself »
Definition and Usage
The array_keys[] function returns an array containing the keys.
Syntax
array_keys[array, value, strict]
Parameter Values
array | Required. Specifies an array |
value | Optional. You can specify a value, then only the keys with this value are returned |
strict | Optional. Used with the value parameter. Possible values:
|
Technical Details
Returns an array containing the keys |
4+ |
The strict parameter was added in PHP 5.0 |
More Examples
Example
Using the value parameter:
Try it Yourself »
Example
Using the strict parameter, false:
Try it Yourself »
Example
Using the strict parameter, true:
Try it Yourself »
❮ PHP Array Reference
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In PHP we can associate name/label with each array elements using => symbol. This is very helpful as it is easy to remember the element because
each element is represented by the label rather than the index value.
Using array_keys[] function: The array_keys[] function is an inbuilt function in PHP which is used to return either all the keys of an array or the subset of the keys.
Syntax:
array array_keys[ $input_array, $search_value, $strict ]
Program 1: Program to get numeric index of associative array using array_keys[] function.
Example 2: Below program uses index to access the values in associative array.