Current Location: Home> Function Categories> array_keys

array_keys

Returns some or all key names in the array
Name:array_keys
Category:Array
Programming Language:php
One-line Description:Returns all key names in the array.

Definition and usage

array_keys() function returns a new array containing all key names in the array.

If a second parameter is provided, only the key name whose key value is that value is returned.

If the strict parameter is specified as true, PHP uses congruent comparisons ( === ) to strictly check the data type of the key value.

Example

Example 1

Returns a new array containing all key names in the array:

 <?php
$a = array ( "Volvo" => "XC90" , "BMW" => "X5" , "Toyota" => "Highlander" ) ;
print_r ( array_keys ( $a ) ) ;
?>

Try it yourself

Example 2

Use the value parameter:

 <?php
$a = array ( "Volvo" => "XC90" , "BMW" => "X5" , "Toyota" => "Highlander" ) ;
print_r ( array_keys ( $a , "Highlander" ) ) ;
?>

Try it yourself

Example 3

Use strict parameter (false):

 <?php
$a = array ( 10 , 20 , 30 , "10" ) ;
print_r ( array_keys ( $a , "10" , false ) ) ;
?>

Try it yourself

Example 4

Use strict parameter (true):

 <?php
$a = array ( 10 , 20 , 30 , "10" ) ;
print_r ( array_keys ( $a , "10" , true ) ) ;
?>

Try it yourself

grammar

 array_keys ( array , value , strict )
parameter describe
array Required. Specify array.
value Optional. You can specify a key value, and then only the key name corresponding to that key value will be returned.
strict

Optional. Used with the value parameter. Possible values:

  • true - Returns the key name with the specified key value. Depend on type, the number 5 is different from the string "5".
  • false - Default value. No dependency on type, the number 5 is the same as the string "5".
Similar Functions
Popular Articles