Php get array key by index


To get the numeric index of an associative array, the code is as follows−

Example

 Live Demo

"5", "b"=>"20", "c"=>"35", "d"=>"55");
   $keys = array_keys( $arr );
   echo "Array key and value...\n";
   for($x = 0; $x < sizeof($arr); $x++ ) {
      echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "\n";
   }
?>

Output

This will produce the following output−

Array key and value...
key: a, value: 5
key: b, value: 20
key: c, value: 35
key: d, value: 55

Example

Let us now see another example−

 Live Demo

"150", "q"=>"100", "r"=>"120", "s"=>"110");
   $keys = array_keys( $arr );
   echo "Array key and value...\n";
   for($x = 0; $x < sizeof($arr); $x++ ) {
      echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "\n";
   }
   $arr[$keys[3]] = "25";
   echo "\nUpdated Array key and value...\n";
   for($x = 0; $x < sizeof($arr); $x++ ) {
      echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "\n";
   }
?>

Output

This will produce the following output−

Array key and value...
key: p, value: 150
key: q, value: 100
key: r, value: 120
key: s, value: 110
Updated Array key and value...
key: p, value: 150
key: q, value: 100
key: r, value: 120
key: s, value: 25

Php get array key by index

Updated on 27-Dec-2019 07:39:23

  • Related Questions & Answers
  • How to access an associative array by integer index in PHP?
  • PHP Associative Array
  • Remove duplicated elements of associative array in PHP
  • How to generate array key using array index – JavaScript associative array?
  • PHP array_push() to create an associative array?
  • How to build dynamic associative array from simple array in php?
  • Convert an object to associative array in PHP
  • PHP Pushing values into an associative array?
  • Associative Arrays in PHP
  • PHP program to add item at the beginning of associative array
  • How to re-index an array in PHP?
  • Return an array with numeric keys PHP?
  • How to use associative array/hashing in JavaScript?
  • Length of a JavaScript associative array?
  • How to get random value out of an array in PHP?

❮ PHP Array Reference

Example

Return an array containing the keys:

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

Try it Yourself »


Definition and Usage

The array_keys() function returns an array containing the keys.


Syntax

array_keys(array, value, strict)

Parameter Values

ParameterDescription
array Required. Specifies an array
value Optional. You can specify a value, then only the keys with this value are returned
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".


Technical Details

Return Value:Returns an array containing the keys
PHP Version:4+
Changelog:The strict parameter was added in PHP 5.0

More Examples

Example

Using the value parameter:

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

Try it Yourself »

Example

Using the strict parameter, false:

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

Try it Yourself »

Example

Using the strict parameter, true:

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

Try it Yourself »


❮ PHP Array Reference


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In PHP we can associate name/label with each array elements using => symbol. This is very helpful as it is easy to remember the element because each element is represented by the label rather than the index value.
    Using array_keys() function: The array_keys() function is an inbuilt function in PHP which is used to return either all the keys of an array or the subset of the keys.

    Syntax:

    array array_keys( $input_array, $search_value, $strict )

    Program 1: Program to get numeric index of associative array using array_keys() function.

    $assoc_array=array("Geeks"=>10, "for"=>15, "geeks"=>20); 

    print_r(array_keys($assoc_array));

    ?>

    Example 2: Below program uses index to access the values in associative array.

    $assoc_array = array(

        "Geeks" => 30,

        "for" => 20,

        "geeks" => 10

    ); 

    $key = array_keys($assoc_array);

    $size = sizeof($key);

    for( $i = 0; $i < $size; $i++) {

        echo "${assoc_array[$key[$i]]}\n";

    }

    ?>


    How do you find array keys?

    The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned. Specified array.

    How do you print the key in an associative array?

    Answer: Use the PHP array_keys() function You can use the PHP array_keys() function to get all the keys out of an associative array.

    How get key of multidimensional array in PHP?

    Retrieving Values: We can retrieve the value of multidimensional array using the following method:.
    Using key: We can use key of the associative array to directly retrieve the data value. ... .
    Using foreach loop: We can use foreach loop to retrieve value of each key associated inside the multidimensional associative array..

    What is $Key in PHP?

    The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns null .