PHP array_keys() Function is an inbuilt function in PHP and return the array of keys from the another given array. Given array may be associative array (key – value) pairs or indexed array. This function return an array.
array array_keys ($array, $search_value, $strict)
PHP array keys Function
$array (required): This argument refers to the main array in which you want to fetch the keys.
$search_value (optional): If this argument is passed then function will return the keys corresponding to this element otherwise it will return all the keys of the array.
$strict (optional): If this argument is true then strict comparison will be used during the search otherwise it has false as default value.
Return Value: This function will return an array of keys like indexed array.
<?php $arr = array("james" => 15, "david" => 18, "smith" => 34); print_r(array_keys($arr)); ?>
Array (  => james  => david  => smith )
<?php $arr = array("david","james","john","smith"); print_r(array_keys($arr)); ?>
Array (  => 0  => 1  => 2  => 3 )
In the below program, passed the search value along with given array. Now it will return only those keys who has the search value.
<?php $arr = array("david","james","david","john","smith","david"); $search_value = "david"; print_r(array_keys($arr,$search_value)); ?>
Array (  => 0  => 2  => 5 )
More Related Post
- PHP Array Functions Examples
- PHP Isset() vs Unset() Function
- PHP String Replace Function
- PHP MVC Framework
- Empty Function In PHP
Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.