PHP array_intersect + array_flip与数组具有多次值 [英] PHP array_intersect + array_flip with array that has values multiple times
本文介绍了PHP array_intersect + array_flip与数组具有多次值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数组:
$arr1 = array(101 => 250, 102 => 250, 103 => 250, 104 => 500, 105 => 500, 106 => 500,);
和
$arr2 = array(0 => 103, 1 => 104, 2 => 105)
我想要得到的结果是
Array (103 => 250, 104 => 500)
我尝试与
I have tried working with
array_intersect(array_flip($arr1), $arr2);
但是
array_flip($arr1)
给出类似的东西
Array(103 => 250, 106 => 500)
因此,密钥丢失并且无法正确相交.有没有办法获得想要的结果?
thus, keys get lost and can not be intersected correctly. Is there a way to get the desired result?
推荐答案
以下代码完成了这项工作.我希望它是不言自明的.
The following code does the job. I hope it is self-explanatory.
array_unique(array_intersect_key($arr1, array_flip($arr2)))
这篇关于PHP array_intersect + array_flip与数组具有多次值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文