PHP array_keys() function

In this chapter you will learn:

  1. Definition for PHP array_keys() function
  2. Syntax for PHP array_keys() function
  3. Parameter for PHP array_keys() function
  4. Example - Get all keys in an array
  5. Example - Get all keys for an associate array
  6. Example - Using the strict parameter, false
  7. Example - Using the strict parameter, true

Definition

The array_keys() function returns an array of all the keys in that array.

Syntax

PHP array_keys() function has the following syntax

array array_keys ( array arr [, mixed value [, bool strict]] )

Parameter

ParameterIs RequiredDescription
arrRequired.Array to search
valueOptional.Only the keys with this value are returned
strictOptional.Used with the value parameter.

Possible values for strict:

ValueDescription
trueCheck type. the number 9 is not the same as the string "9".
falseDefault value. Not checking type, the number 9 is the same as the string "9".

By default, this is done by checking each key's value with the == operator (is equal to); however, if you specify 1 as the third parameter, the check will be done with === (identical to).

Example

Get all keys in an array


<?php//j  av  a  2s .c o  m
    $users[0] = 'PHP';
    $users[1] = 'Java';
    $users[2] = 'java2s.com';
    $userids = array_keys($users);
    print_r($userids);
?>

The code above generates the following result.

Example 2

Using the value parameter:


<?php
    $a=array("p"=>"PHP","j"=>"java2s.com","a"=>"Apple");
    print_r(array_keys($a,"java2s.com"));
?>

The code above generates the following result.

Example 3

Using the strict parameter, false:


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

The code above generates the following result.

Example 4

Using the strict parameter, true:


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

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP array_map() Function
  2. Syntax for PHP array_map() Function
  3. Parameter for PHP array_map() Function
  4. Example - Map an array with user function
  5. Example - Using a user-made function to change the values of an array
  6. Example - Change all letters of the array values to uppercase
  7. Example - Assign null as the function name
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function