array_flip() - 交换数组中的键和值 - php 数组函数
array_flip()
(PHP 4, PHP 5, PHP 7)
交换数组中的键和值
说明
array_flip(array $array): arrayarray_flip()返回一个反转后的array,例如$array中的键名变成了值,而$array中的值成了键名。
注意$array中的值需要能够作为合法的键名(例如需要是integer或者string)。如果类型不对,将出现一个警告,并且有问题的键/值对将不会出现在结果里。
如果同一个值出现多次,则最后一个键名将作为它的值,其它键会被丢弃。
参数
$array要交换键/值对的数组。
返回值
成功时返回交换后的数组,如果失败返回NULL
。
范例
Example #1array_flip()例子
以上例程会输出:
Array ( [oranges] => 0 [apples] => 1 [pears] => 2 )
Example #2array_flip()例子:冲突
以上例程会输出:
Array ( [1] => b [2] => c )
参见
array_values()
返回数组中所有的值array_keys()
返回数组中部分的或所有的键名array_reverse()
返回单元顺序相反的数组
I find this function vey useful when you have a big array and you want to know if a given value is in the array. in_array in fact becomes quite slow in such a case, but you can flip the big array and then use isset to obtain the same result in a much faster way.
This function is useful when parsing a CSV file with a heading column, but the columns might vary in order or presence: I find this better than referencing the numerical array index.
array_flip() does not retain the data type of values, when converting them into keys. :( This code outputs this: array(3) { ["one"]=> string(1) "1" ["two"]=> string(1) "2" ["three"]=> string(1) "3" } array(3) { [1]=> string(3) "one" [2]=> string(3) "two" [3]=> string(5) "three" } It is valid expectation that string values "1", "2" and "3" would become string keys "1", "2" and "3".
I needed a way to flip a multidimensional array and came up with this function to accomplish the task. I hope it helps someone else.
When you do array_flip, it takes the last key accurence for each value, but be aware that keys order in flipped array will be in the order, values were first seen in original array. For example, array: [1] => 1 [2] => 2 [3] => 3 [4] => 3 [5] => 2 [6] => 1 [7] => 1 [8] => 3 [9] => 3 After flipping will become: (first seen value -> first key) [1] => 7 [2] => 5 [3] => 9 And not anything like this: (last seen value -> last key) [2] => 5 [1] => 7 [3] => 9 In my application I needed to find five most recently commented entries. I had a sorted comment-id => entry-id array, and what popped in my mind is just do array_flip($array), and I thought I now would have last five entries in the array as most recently commented entry => comment pairs. In fact it wasn't (see above, as it is the order of values used). To achieve what I need I came up with the following (in case someone will need to do something like that): First, we need a way to flip an array, taking the first encountered key for each of values in array. You can do it with: $array = array_flip(array_unique($array)); Well, and to achieve that "last comments" effect, just do: $array = array_reverse($array, true); $array = array_flip(array_unique($array)); $array = array_reverse($array, true); In the example from the very beginning array will become: [2] => 5 [1] => 7 [3] => 9 Just what I (and maybe you?) need. =^_^=
In case anyone is wondering how array_flip() treats empty arrays: results in: Array ( ) I wanted to know if it would return false and/or even chuck out an error if there were no key-value pairs to flip, despite being non-intuitive if that were the case. But (of course) everything works as expected. Just a head's up for the paranoid.
Notice : array_flip can turn string into integer
Finding the longest string in an array? Differences are obvious: returns an array of [i]all[/i] of the longest strings, instead of just picking one arbitrarily. Doesn't do the stripslashing or magic stuff because that's another job for for another function.
The above example will output: array(3) { ["one"]=> array(1) { ["four"]=> int(4) } ["two"]=> string(1) "2" ["three"]=> string(1) "3" } Warning: array_flip(): Can only flip STRING and INTEGER values! in /root/test.php on line 4 array(2) { [2]=> string(3) "two" [3]=> string(5) "three" }
Don't use this function for filtering or searching an array - PHP already has functions for exactly those purposes. If nothing else, array_flip will trash the array's elements if they're anything other than integers or non-decimal-integer strings.
array_flip will remove duplicate values in the original array when you flip either an associative or numeric array. As you might expect it's the earlier of two duplicates that is lost: Result: array(3) { [0] => string(3) "one" [1] => string(3) "two" [2] => string(3) "one" } array(2) { 'one' => int(2) 'two' => int(1) } This may be good or bad, depending on what you want, but no error is thrown.
note :: array_flip is a changer for key and value and a auto unique like array_unique :
Similarly, if you want the last value without affecting the pointer, you can do:
From an algorithmic efficiency standpoint, building an entire array of lengths to then sort to only retrieve the longest value is unnecessary work. The following should be O(n) instead of O(n log n). It could also be:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)