php 数组key键比较,PHP 使用键名比较计算数组的交集

用户评论:

vladas dot dirzys at gmail dot com (2012-08-13 08:03:59)

Simple key white-list filter:

$arr= array('a'=>123,'b'=>213,'c'=>321);$allowed= array('b','c');print_r(array_intersect_key($arr,array_flip($allowed)));?>

Will return:

Array

(

[b] => 213

[c] => 321

)

pgl at yoyo dot org (2011-07-18 08:01:11)

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:

$array= array('two'=>'a','three'=>'b','one'=>'c',

);$keyswant= array('one'=>'','three'=>'',

);print_r(array_intersect_key($array,$keyswant));?>

Shows:

Array

(

[three] => b

[one] => c

)

chrisbloom7 at gmail dot com (2009-11-11 10:23:06)

Regarding php at keithtylerdotcom solution to emulate

$z=someFuncReturningAnArray()['some_key'];?>

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:

return array('a'=>'b','c'=>'d','e'=>'f','g'=>'h','i'=>'j');

}//traditional way$temp=someFuncReturningAnArray();$b=$temp['a'];

echoprint_r($b,1) ."\n----------\n";//keithtylerdotcom one-line method$b=array_intersect_key(someFuncReturningAnArray(), array('a'=>''));

echoprint_r($b,1) ."\n----------\n";//better one line method$b=implode('',array_intersect_key(someFuncReturningAnArray(), array('a'=>'')));

echoprint_r($b,1) ."\n----------\n";?>

markus dot kappe at dix dot at (2009-09-24 04:43:56)

* calculates intersection of two arrays like array_intersect_key but recursive

*

* @param  array/mixed  master array

* @param  array        array that has the keys which should be kept in the master array

* @return array/mixed  cleand master array

*/functionmyIntersect($master,$mask) {

if (!is_array($master)) { return$master; }

foreach ($masteras$k=>$v) {

if (!isset($mask[$k])) { unset ($master[$k]); continue; }// remove value from $master if the key is not present in $maskif (is_array($mask[$k])) {$master[$k] =$this->myIntersect($master[$k],$mask[$k]); }// recurse when mask is an array

// else simply keep value}

return$master;

}?>

pdemaziere at gmail dot com (2009-02-23 07:52:26)

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.

$outcome=array_values(array_intersect_key(array_values($source),array_filter(array_values($mask)) ));?>

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!

CBWhiz at gmail dot com (2008-01-04 14:04:30)

I have found the following helpful:

<?PHPfunctionarray_merge_default ($default,$data) {$intersect=array_intersect_key($data,$default);//Get data for which a default exists$diff=array_diff_key($default,$data);//Get defaults which are not present in datareturn$diff+$intersect;//Arrays have different keys, return the union of the two}?>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:

$default= array("one"=>1,"two"=>2);$untrusted= array("one"=>42,"three"=>3);var_dump(array_merge_default($default,$untrusted));

array(2) {

["two"]=>int(2)

["one"]=>int(42)

}?>

Rod Byrnes (2007-05-05 21:10:42)

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.

{

functionarray_intersect_key($isec,$keys)

{$argc=func_num_args();

if ($argc>2)

{

for ($i=1; !empty($isec) &&$i

{$arr=func_get_arg($i);

foreach (array_keys($isec) as$key)

{

if (!isset($arr[$key]))

{

unset($isec[$key]);

}

}

}

return$isec;

}

else

{$res= array();

foreach (array_keys($isec) as$key)

{

if (isset($keys[$key]))

{$res[$key] =$isec[$key];

}

}

return$res;

}

}

}?>

(2006-07-17 05:31:26)

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;

}

}

Anton Backer (2006-03-30 23:49:50)

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.

dak (2006-01-23 20:31:45)

A more efficient (and, I think, simpler) compatibility implementation:

{

functionarray_intersect_key($isec,$arr2)

{$argc=func_num_args();

for ($i=1; !empty($isec) &&$i

{$arr=func_get_arg($i);

foreach ($isecas$k=>&$v)

if (!isset($arr[$k]))

unset($isec[$k]);

}

return$isec;

}

}?>

Silvio Ginter (2005-09-23 05:17:28)

Based on the code posted by gaylord dot aulke at 100days.de

i wrote this one. This should implement this function in all versions equal or greater than PHP 4.0

function array_intersect_key($arr1, $arr2) {

$res = array();

foreach($arr1 as $key=>$value) {

$push = true;

for ($i = 1; $i < func_num_args(); $i++) {

$actArray = func_get_arg($i);

if (gettype($actArray) != 'array') return false;

if (!array_key_exists($key, $actArray)) $push = false;

}

if ($push) $res[$key] = $arr1[$key];

}

return $res;

}

gaylord dot aulke at 100days.de (2005-07-04 04:04:02)

I tried to use this function with PHP 5.0.4 under windows but the function does not seem to be implemented.

(Fatal error: Call to undefined function array_intersect_key())

This works as a workaround for 2 arrays at least:

function array_intersect_key($arr1, $arr2) {

$res = array();

foreach($arr1 as $key=>$value) {

if(array_key_exists($key, $arr2)) $res[$key] = $arr1[$key];

}

return $res;

}

aidan at php dot net (2005-05-29 08:51:02)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值