array_keys() function in PHP


The array_keys() function returns all the keys of an array. It returns an array of all the keys in array.

Syntax

array_keys(arr, value, strict)

Parameters

  • arr − The array from which the keys will be returned

  • value − If the values are specified, then only the keys containing these values will be returned.

  • strict − Determines if strict comparison (===) should be used during the search.

Return

The array_keys() function returns an array of all the keys in array.

Example

 Live Demo

<?php
$arr = array("one" => "Pen", "two" => "Pencil", "three" => "Paper", "four" => "Notepad");
print_r(array_keys($arr));
?>

Output

Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)

Example

Let us see another example wherein we will find the key for a specific value “Notepad”.

 Live Demo

<?php
$arr = array("one" => "Pen", "two" => "Notepad", "three" => "Paper", "four" => "Notepad");
print_r(array_keys($arr, "Notepad"));
?>

Output

Array
(
[0] => two
[1] => four
)

Updated on: 23-Dec-2019

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements