PHP - Function array_keys()


Advertisements


Syntax

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

Definition and Usage

Returns the keys, numeric and string, from the input array. If the optional search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the input are returned.

Parameters

Sr.No Parameter & Description
1

input

Required.Specifies an array.

2

search_value

Required. You can specify a value, then only the keys with this value are returned.

3

strict

Optional. Used with the value parameter. Possible values:

* true - Returns the keys with the specified value, depending on type: the number 5 is not the same as the string "5".

* false - Default value. Not depending on type, the number 5 is the same as the string "5".

Return Values

Returns the keys, numeric and string, from the input array

Example

Try out following example −

<?php
   $a = array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
   print_r(array_keys($a));
   
   $a = array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
   print_r(array_keys($a,"Dog"));
   
   $a = array(10,20,30,"10");
   print_r(array_keys($a,"10",false));
?> 

This will produce the following result −

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

php_function_reference.htm