array_keys
PHP array_keys()函数 (PHP array_keys() function)
array_keys() function is used to get the keys of an array, it accepts an array as an argument and returns a new array containing keys.
array_keys()函数用于获取数组的键,它接受一个数组作为参数并返回一个包含键的新数组。
Syntax:
句法:
array_keys(input_array, [value], [strict]);
Here,
这里,
input_array is an array (i.e. input array).
input_array是一个数组(即输入数组)。
value is an optional parameter, it is used to define a value if the value is defined, then the only keys having that value are returned.
value是一个可选参数,如果定义了值,则用于定义值,然后仅返回具有该值的键。
strict is also an optional parameter, it is default set to false if we set it true the type of values will be checked.
严格也是一个可选的参数,它是默认设置为false,如果我们把它真值的类型将被检查。
Examples:
例子:
Input:
$per = array("name" => "Amit", "age" => 21, "gender" => "Male");
Output:
Array
(
[0] => name
[1] => age
[2] => gender
)
PHP code 1: array with and without containing keys
PHP代码1:包含和不包含键的数组
<?php
$per = array("name" => "Amit", "age" => 21, "gender" => "Male");
print ("keys array...\n");
print_r (array_keys($per));
//array with out keys
$arr = array("Hello", "world", 100, 200, -10);
print ("keys array...\n");
print_r (array_keys($arr));
?>
Output
输出量
keys array...
Array
(
[0] => name
[1] => age
[2] => gender
)
keys array...
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
PHP code 2: Using value and strict mode
PHP代码2:使用值和严格模式
<?php
$arr = array("101" => 100, "102" => "100", "103" => 200);
print("output (default function call)...\n");
print_r (array_keys($arr));
print("output (with checking value)...\n");
print_r (array_keys($arr, 100));
print("output (with checking value & strict mode)...\n");
print_r (array_keys($arr, 100, true));
?>
Output
输出量
output (default function call)...
Array
(
[0] => 101
[1] => 102
[2] => 103
)
output (with checking value)...
Array
(
[0] => 101
[1] => 102
)
output (with checking value & strict mode)...
Array
(
[0] => 101
)
翻译自: https://www.includehelp.com/php/array_keys-function-with-example.aspx
array_keys