PHP array_keys() function
In this chapter you will learn:
- Definition for PHP array_keys() function
- Syntax for PHP array_keys() function
- Parameter for PHP array_keys() function
- Example - Get all keys in an array
- Example - Get all keys for an associate array
- Example - Using the strict parameter, false
- 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
Parameter | Is Required | Description |
---|---|---|
arr | Required. | Array to search |
value | Optional. | Only the keys with this value are returned |
strict | Optional. | Used with the value parameter. |
Possible values for strict:
Value | Description |
---|---|
true | Check type. the number 9 is not the same as the string "9". |
false | Default 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:
- Definition for PHP array_map() Function
- Syntax for PHP array_map() Function
- Parameter for PHP array_map() Function
- Example - Map an array with user function
- Example - Using a user-made function to change the values of an array
- Example - Change all letters of the array values to uppercase
- Example - Assign null as the function name
Home » PHP Tutorial » PHP Array Functions