有些简单的PHP数组相交问题 [英] Somewhat simple PHP array intersection question

查看:124
本文介绍了有些简单的PHP数组相交问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我要疯了,但我可以发誓,有历时两个数组作为参数的PHP核心功能:

Maybe I'm going insane, but I could have sworn that there was an PHP core function which took two arrays as arguments:

$a = array('1', '3');
$b = array('1'=>'apples', '2'=>'oranges', '3'=>'kiwis');

和执行,其中从数组中的值 $ A 检查碰撞与数组键路口 $ B 。返回类似

And performs an intersection where the values from array $a are checked for collisions with the keys in array $b. Returning something like

array('1'=>'apples', '3'=>'kiwis');

有没有这样的功能存在(这是我错过了文档中),或者是有一个非常优化的方式来达到同样的事情?

Does such a function exist (which I missed in the documentation), or is there a very optimized way to achieve the same thing?

推荐答案

尝试使用array_flip {按键开关与他们的价值观},然后使用array_intersect()
在你的例子:

try using array_flip {switches keys with their values} and then use array_intersect() on your example :

$c = array_flip($b); // so you have your original b-array
$intersect = array_intersect($a,c);

这篇关于有些简单的PHP数组相交问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆