PHP | array_keys() Function - GeeksforGeeks

11 Nov, 2021
The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys.
Syntax:
array array_keys($input_array, $search_value, $strict)
Parameters: The function takes three parameters out of which one is mandatory and other two are optional.
$input_array (mandatory): Refers to the array that we want to operate on.$search_value (optional): Refers to the value of the array by which we want to search the array for the key elements. If this parameter is passed then the function will return keys corresponding to this element only otherwise it will return all keys of the array.$strict (optional): Determines if strict comparison (===) should be used during the search. false is the default value.
$input_array (mandatory): Refers to the array that we want to operate on.
$search_value (optional): Refers to the value of the array by which we want to search the array for the key elements. If this parameter is passed then the function will return keys corresponding to this element only otherwise it will return all keys of the array.
$strict (optional): Determines if strict comparison (===) should be used during the search. false is the default value.
Return Value: The function returns an array containing either all of the keys or subset of keys the input array depending upon the parameters passed.
Examples:
Input :  $input_array = ("one" => "shyam", 2 => "rishav", 
                                          "three" => "gaurav")         
Output :
Array
(
    [0] => one
    [1] => 2
    [2] => three
)

Input : $input_array = ("one", "two", "three", "one", 
                          "four", "three", "one", "one")
        $search_value = "one"
Output :
Array
(
    [0] => 0
    [1] => 3
    [2] => 6
    [3] => 7
)

In the below program, we have passed a simple associative array to the function array_keys(), to print all of its keys:
<?php  // PHP function to illustrate the use of array_keys()function get_Key($array){    $result = array_keys($array);    return($result);}  $array = array("one" => "shyam", 2 => "rishav",                              "three" => "gaurav");print_r(get_Key($array));  ?>
Output:
Array
(
    [0] => one
    [1] => 2
    [2] => three
)

In the below program, along with the array we have passed a value only for which the key position is returned.
<?php  // PHP function to illustrate the use of array_keys()function get_Key($array, $search_value){    $result = array_keys($array, $search_value);    return($result);}  $array = array("one", "two", "three", "one", "four",                                "three", "one", "one");$search_value = "one";print_r(get_Key($array, $search_value));  ?>
Output:
Array
(
    [0] => 0
    [1] => 3
    [2] => 6
    [3] => 7
)

Reference: http://php.net/manual/en/function.array-keys.php
surindertarika1234
PHP-array
PHP-function
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
PHP | Converting string to Date and DateTime
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills