array_keys to get the keys of matching values of an array

array_keys(): collect array keys

array_keys() returns all keys from an array. You can also pass a search value and optionally request strict comparison.

$user = ['name' => 'Ravi', 'city' => 'Hyderabad', 'active' => true];
$keys = array_keys($user);
print_r($keys);

The returned array is numerically indexed even when the source array is associative.

array = array_keys ($input_array, $search_str,$strict);
ParameterDESCRIPTION
$input_arrayRequired : Input array to be searched.
$search_strOptional: Values to be searched inside $input_array.
$strictOptional: FALSE ( default): strict comparison is not done.
TRUE comparison of type of variable is done.
arrays_key function returns an array with all the keys as element. We can specify a value of the array and get the associated keys for the element.

We will start with some examples.
$ar=array("SName"=>"Ronald","Class"=>"Fourth","Subject"=>"Science","Game"=>"Cricket");
$kar=array_keys($ar);
print_r($kar);
The output is here
Array ( [0] => SName [1] => Class [2] => Subject [3] => Game )
We can pass a value (of element ) and get the respective key like this
$ar=array("SName"=>"Ronald","Class"=>"Fourth","Subject"=>"Science","Game"=>"Cricket");
$kar=array_keys($ar,'Fourth');
print_r($kar);
We will get a single element array returning the key Output is here
Array ( [0] => Class )

Example with more than one matching

$ar=array("SName"=>"Ronald","Class"=>"Fourth","Subject"=>"Cricket","Game"=>"Cricket");
$kar=array_keys($ar,'Cricket');
print_r($kar);
Output is here
Array ( [0] => Subject [1] => Game )
Two keys are returned in above case.
FunctionDetails
in_array()Search for value inside Array. Returns TRUE or FALSE
array_search()Search for value inside Array. Returns the key if found, FALSE otherwise
array_key_exists()Search for key inside Array. True if found, FALSE otherwise
Seach for keys inside an array
Array REFERENCE


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer