Sunday 25 August 2013

PHP Function array_keys()

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.

Paramters

ParameterDescription
inputRequired.Specifies an array.
search_valueRequired. You can specify a value, then only the keys with this value are returned.
strictOptional. 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 following result:
Array ( [0] => a [1] => b [2] => c )
Array ( [0] => c)
Array ( [0] => 0 [1] => 3 )

No comments:

Post a Comment