array_intersect_key() - 使用键名比较计算数组的交集 - php 数组函数
array_intersect_key()
(PHP 5 >= 5.1.0, PHP 7)
使用键名比较计算数组的交集
说明
array_intersect_key(array $array1,array $array2[,array $...]): arrayarray_intersect_key()返回一个数组,该数组包含了所有出现在$array1中并同时出现在所有其它参数数组中的键名的值。
参数
$array1The array with master keys to check.
$array2An array to compare keys against.
A variable list of arrays to compare.
返回值
Returns an associative array containing all the entries of$array1which have keys that are present in all arguments.
范例
Example #1array_intersect_key()例子
以上例程会输出:
array(2) { ["blue"]=> int(1) ["green"]=> int(3) }
上例中可以看到只有'blue'和'green'两个键名出现在两个数组中,因此被返回。此外注意'blue'和'green'的值在两个数组中是不同的。但因为只检查键名,因此还是匹配。返回的值只是$array1中的。
在key => value对中的两个键名仅在(string)$key1 ===(string)$key2时被认为相等。换句话说,执行的是严格类型检查,因此字符串的表达必须完全一样。
参见
array_diff()
计算数组的差集array_udiff()
用回调函数比较数据来计算数组的差集array_diff_assoc()
带索引检查计算数组的差集array_diff_uassoc()
用用户提供的回调函数做索引检查来计算数组的差集array_udiff_assoc()
带索引检查计算数组的差集,用回调函数比较数据array_udiff_uassoc()
带索引检查计算数组的差集,用回调函数比较数据和索引array_diff_key()
使用键名比较计算数组的差集array_diff_ukey()
用回调函数对键名比较计算数组的差集array_intersect()
计算数组的交集array_intersect_assoc()
带索引检查计算数组的交集array_intersect_uassoc()
带索引检查计算数组的交集,用回调函数比较索引array_intersect_ukey()
用回调函数比较键名来计算数组的交集
Simple key white-list filter: Will return: Array ( [b] => 213 [c] => 321 )
[Editor's note: changed array_merge_recursive() to array_replace_recursive() to fix the script] Here is a better way to merge settings using some defaults as a whitelist. Output: array (size=4) 'id' => int 123456 'client_id' => int 789 'client_secret' => string '5ebe2294ecd0e0f08eab7690d2a6ee69' (length=32) 'options' => array (size=2) 'trusted' => boolean false 'active' => boolean true
Note that the order of the keys in the returned array is the same as the order of the keys in the source array. eg: Shows: Array ( [three] => b [one] => c )
This functionality is now implemented in the PEAR package PHP_Compat. More information about using this function without upgrading your version of PHP can be found on the below link: http://pear.php.net/package/PHP_Compat
I have found the following helpful: It's use is like both of the functions it uses, but keeps defaults and _only_ defaults. It's designed for key arrays, and i'm not sure how it will work on numeric indexed arrays. Example:
Jesse: no, array_intersect_key does not accomplish the same thing as what you posted: array_flip (array_intersect (array_flip ($a), array_flip ($b))) because when the array is flipped, values become keys. having duplicate values is not a problem, but having duplicate keys is. array_flip resolves it by keeping only one of the duplicates and discarding the rest. by the time you start intersecting, you've already lost information.
If you want an array that has no key value pairs added from the second array: $new = array_intersect_key($b, $a) + $a;
Regarding php at keithtylerdotcom solution to emulate His recommended solution will still return an array. To get the value of a single key in an array returned by a function, simply add implode() to the recipe:
Just a simple script if you want to use one array, which contains only zeros and ones, as mask for another one (both arrays must have the same size of course). $outcome is an array that contains only those values from $source where $mask is equal to 1. PS: the array_values() function is necessary to ensure that both arrays have the same numbering/keys, otherwise your masking does not behave as you expect. Enjoy!
in case you came here looking for a function that returns an array containing the values of `all` arrays with intersecting keys:
Here is a faster version than those shown below, with optimisation for the case when only two arrays are passed. In my tests with a 10000 item first array and a 5000 item second array (run 20 times) this function ran in 1.89 seconds compared with 2.66 for the version posted by dak. For a three array case, same as above but with the third array containing 3333 values, the timing is 3.25 for this version compared with 3.7 for dak's version.
Here it is a more obvious way to implement the function: if (!function_exists('array_intersect_key')) { function array_intersect_key() { $arrs = func_get_args(); $result = array_shift($arrs); foreach ($arrs as $array) { foreach ($result as $key => $v) { if (!array_key_exists($key, $array)) { unset($result[$key]); } } } return $result; } }
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!