Home > headlines > body text

Detailed explanation of array_keys() function in php

PHP中文网
Release: 2017-11-01 09:48:40
Original
2514 people have browsed it

The array_keys() function returns an array containing all keys found in the searched array. Its form is as follows:

array array_keys(array array[,mixed search_value])

If the optional parameter search_value is included, only keys matching the value will be returned. The following example will output all arrays found in the $fruit array:

$fruits["apple"] = "red";  
$fruits["banana"] = "yellow";  
$fruits["watermelon"]="green";  
$keys = array_keys($fruits);  
print_r($keys);  
  
//Array ( [0] => apple [1] => banana [2] => watermelon )
Copy after login

Returns a new array containing all the key names in the array:

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
?>
Copy after login

Definition and Usage

array_keys () function returns a new array containing all the keys in the array.

Syntax

array_keys(array,value,strict)
Copy after login

array Required. Specifies an array.

value Optional. You can specify a key value, and then only the key name corresponding to that key value will be returned.

strict Optional. Used with the value parameter. Possible values:

true - Returns the key name with the specified key value. Depending on the type, the number 5 is not the same as the string "5".

false - the default value. Independent of type, the number 5 is the same as the string "5".

Return value:

Returns a new array containing all the key names in the array.

Parameters:

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a,"Highlander"));
?>
Copy after login

Use strict parameter (false):

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

Use strict parameter (true):

<?php$a=array(10,20,30,"10");print_r(array_keys($a,"10",true));?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!