array_keys 返回数组中部分的或所有的键名
$array
): array$array
, mixed $search_value
, bool $strict
= false
): arrayinput
search_value
strict
1、返回数据所有的key
<?php $array = array(0 => 100, "color" => "red"); print_r(array_keys($array));// $key = 2; // $key = 1; ?>
返回:
Array ( [0] => 0 [1] => color )
2、返回数据中指定值的所有key
<?php $array = array("blue", "red", "green", "blue", "blue"); print_r(array_keys($array, "blue"));$key = 2; // $key = 1; ?>
返回:
Array ( [0] => 0 [1] => 3 [2] => 4 )
3、默认校验大小写的值查找
<?php $array = array("Blue", "red", "green", "blue", "Blue"); print_r(array_keys($array, "blue", true)); // $key = 1; ?>
返回:只返回了小写blue的key
Array ( [0] => 3 )
4、严格校验值类型,设置第三个参数为true
<?php $array = array("", 0,); print_r(array_keys($array, 0)); print_r(array_keys($array, 0, true)); // $key = 1; ?>
返回: 在php中0==”“ ,加上严格类型匹配,则会强制校验数据类型
Array ( [0] => 0 [1] => 1 ) Array ( [0] => 1 )
5、只能支持二位数组查找值,对于下面查找haha,则会返回空
<?php $array = array("", 0 => array(‘haha‘, ‘hehe‘)); print_r(array_keys($array, ‘haha‘)); // $key = 1; ?>
6、查钊的值可以是复杂的数组类型,例如array(‘haha‘, ‘hehe‘)
<?php $array = array("", ”key“ => array(‘haha‘, ‘hehe‘)); print_r(array_keys($array, array(‘haha‘, ‘hehe‘))); // $key = 1; ?>
返回:
Array ( [0] => ”key“ )
原文:https://www.cnblogs.com/ttaylor/p/15093606.html