array_keys() Function

The array_keys() function is used to return all the keys or a subset of the array keys. This function works for both indexed as well as associative arrays. 

Syntax

array_keys(array, value, strict);

Parameters 

  • array: An array with keys to check.
  • value: The value to retrieve the keys for.
  • strict: The parameter to check the data type of the variable or not.

Example 1: This example illustrates the basic usage of the array_keys() Function in PHP.

PHP




<?php
$arr = array(
    "Java" => "SpringBoot",
    "PHP 4.0" => "CodeIgniter",
    "Python" => "Django",
    "PHP 3.0" => "CodeIgniter"
);
 
// Searching for keys of codeigniter
$key1 = array_keys($arr, "CodeIgniter");
print("Keys for CodeIgniter : ");
print_r($key1);
print("</br>");
 
// Searching for keys of wordpress
$key2 = array_keys($arr, "WordPress");
print("Keys for WordPress : ");
print_r($key2);
?>


Output:

Keys for CodeIgniter : Array ( [0] => PHP 4.0 [1] => PHP 3.0 )
Keys for WordPress : Array ( )

Example 2: This is another example that illustrates the basic usage of the array_keys() Function in PHP.

PHP




<?php
$arr = array(1, 2, 3, 4, 5);
 
// Searching for keys of string 5
// using strict parameter true
$key1 = array_keys($arr, "5", true);
print ("Keys for '5' : ");
print_r($key1);
echo ("</br>");
 
// Searching for keys of string 5
// using strict parameter false
$key2 = array_keys($arr, "5", false);
print ("Keys for '5' : ");
print_r($key2);
?>


Output:

Keys for '5' : Array ( )
Keys for '5' : Array ( [0] => 4 )

What are the differences between array_keys() and array_key_exists() in PHP ?

The following article indicates the differences between the two inbuilt functions array_keys() and array_key_exists() in PHP.

Similar Reads

array_keys() Function

The array_keys() function is used to return all the keys or a subset of the array keys. This function works for both indexed as well as associative arrays....

array_key_exists() Function

...