English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

Usage and example of PHP array_keys() function

PHP Array Function Manual

The PHP array_keys() function returns partial or all key names in the array

Syntax

array_keys( $input[, $search_value[, $strict]]);

Definition and Usage

array_keys() returns the key names of numbers or strings in the input array.
If the optional parameter search_value is specified, only the key names of that value are returned. Otherwise, all key names in the input array are returned.

Parameter

Serial NumberParameters and Description
1

input(Required)

It specifies an array.

2

search_value(Required)

You can specify a value and only return the keys that have that value.

3

strict

Optional. Used together with the value parameter.

Return value

It returns keys, numbers, and strings from the $input array

Online example

Returns all keys in the array and the key of the specified value

<?php
   $input = array("a" => "Monkey", "b" => "Cat", "c" => "Dog");
   print_r(array_keys($input));
   
   $input = array("a" => "Monkey", "b" => "Cat", "c" => "Dog");
   print_r(array_keys($input, "Dog"));
   
   $input = array(10,20,30, "10");
   print_r(array_keys($input, "10", false));
?>
Test and see‹/›

Output result:

Array
(
    [0] => a
    [1] => b
    [2] => c
)
Array
(
    [0] => c
)
Array
(
    [0] => 0
    [1] => 3
)

PHP Array Function Manual