array_keys() - 返回数组中部分的或所有的键名 - php 数组函数
array_keys()
(PHP 4, PHP 5, PHP 7)
返回数组中部分的或所有的键名
说明
array_keys(array $array[,mixed $search_value= null[,bool $strict= false]]) : arrayarray_keys()返回$input数组中的数字或者字符串的键名。
如果指定了可选参数$search_value,则只返回该值的键名。否则$input数组中的所有键名都会被返回。
参数
$input
一个数组,包含了要返回的键。
$search_value如果指定了这个参数,只有包含这些值的键才会返回。
$strict判断在搜索的时候是否该使用严格的比较(===)。
返回值
返回$input里的所有键。
范例
Example #1array_keys()例子
以上例程会输出:
Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size )
参见
array_values()
返回数组中所有的值array_combine()
创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值array_key_exists()
检查数组里是否有指定的键名或索引array_search()
在数组中搜索给定的值,如果成功则返回首个相应的键名
It's worth noting that if you have keys that are long integer, such as '329462291595', they will be considered as such on a 64bits system, but will be of type string on a 32 bits system. for example: will return on a 64 bits system: but on a 32 bits system: I hope it will save someone the huge headache I had :)
Here's how to get the first key, the last key, the first value or the last value of a (hash) array without explicitly copying nor altering the original array:
Since 5.4 STRICT standards dictate that you cannot wrap array_keys in a function like array_shift that attempts to reference the array. Invalid: echo array_shift( array_keys( array('a' => 'apple') ) ); Valid: $keys = array_keys( array('a' => 'apple') ); echo array_shift( $keys ); But Wait! Since PHP (currently) allows you to break a reference by wrapping a variable in parentheses, you can currently use: echo array_shift( ( array_keys( array('a' => 'apple') ) ) ); However I would expect in time the PHP team will modify the rules of parentheses.
It is worth noting that array_keys does not maintain the data-type of the keys when mapping them to a new array. This created an issue with in_array and doing a lookup on characters from a string. NOTE: my lookup $array has a full map of numbers and characters - upper and lower - to do an simple faux encryption with. Outputs: array (size=4) 'e' => string 'ieio' (length=4) 1 => string 'one' (length=3) 2 => string 'two' (length=3) 0 => string 'zero' (length=4) array (size=4) 0 => string 'e' (length=1) 1 => int 1 2 => int 2 3 => int 0 sweet dude sweet ---- expected to see: dude dude dude
There's a lot of multidimensional array_keys function out there, but each of them only merges all the keys in one flat array. Here's a way to find all the keys from a multidimensional array while keeping the array structure. An optional MAXIMUM DEPTH parameter can be set for testing purpose in case of very large arrays. NOTE: If the sub element isn't an array, it will be ignore. EXAMPLE: input: array( 'Player' => array( 'id' => '4', 'state' => 'active', ), 'LevelSimulation' => array( 'id' => '1', 'simulation_id' => '1', 'level_id' => '1', 'Level' => array( 'id' => '1', 'city_id' => '8', 'City' => array( 'id' => '8', 'class' => 'home', ) ) ), 'User' => array( 'id' => '48', 'gender' => 'M', 'group' => 'user', 'username' => 'Hello' ) ) output: array( 'Player' => array(), 'LevelSimulation' => array( 'Level' => array( 'City' => array() ) ), 'User' => array() )
If an array is empty (but defined), or the $search_value is not found in the array, an empty array is returned (not false, null, or -1). This may seem intuitive, especially given the documentation says an array is returned, but I needed to sanity test to be sure:
Sorry for my english... I wrote a function to get keys of arrays recursivelly... I hope it will be usefull Regards
An alternative to RQuadling at GMail dot com's array_remove() function:
The position of an element. One can apply array_keys twice to get the position of an element from its key. (This is the reverse of the function by cristianDOTzuddas.) E.g., the following may output "yes, we have bananas at position 0". Not amazingly efficient, but I see no better alternative.
Here's a function I needed to collapse an array, in my case from a database query. It takes an array that contains key-value pairs and returns an array where they are actually the key and value. Example usage (pseudo-database code): I found this handy for using with json_encode and am using it for my project http://squidby.com
might be worth noting in the docs that not all associative (string) keys are a like, output of the follow bit of code demonstrates - might be a handy introduction to automatic typecasting in php for some people (and save a few headaches):
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)