array_keys() – Returns All or Selected Keys of an Array (PHP)
This article explains the PHP array_keys() function, detailing its purpose of returning numeric or string keys from an input array, describing optional parameters search_value and strict, and providing multiple code examples that illustrate basic usage, filtered key retrieval, and handling of associative arrays.
The array_keys() function in PHP returns the keys of an array, which can be either numeric or string.
If the optional search_value parameter is supplied, only the keys whose corresponding values match search_value are returned; otherwise all keys are returned. The optional strict boolean determines whether the comparison uses strict identity ( === ).
Parameters
input (array): The array from which to retrieve keys.
search_value (mixed, optional): The value to search for; only keys of elements equal to this value are returned.
strict (bool, optional): When true , the comparison is strict ( === ).
Return value
Returns an array containing the keys from input that meet the criteria.
Example 1 – Basic usage
100,
"color" => "red"
);
print_r(array_keys($array));
?>Output:
Array
(
[0] => 0
[1] => color
)Example 2 – Using search_value
Output:
Array
(
[0] => 0
[1] => 2
)Example 3 – Using strict comparison
array("blue", "red", "green"),
"size" => array("small", "medium", "large")
);
print_r(array_keys($array, "blue", true));
?>Output:
Array
(
[0] => color
)Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.