PHP比较两个数组并获得匹配的值而不是差异 [英] PHP compare two arrays and get the matched values not the difference

查看:30
本文介绍了PHP比较两个数组并获得匹配的值而不是差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较两个数组并仅获取两个数组中存在的值,但不幸的是,我找不到要使用的正确数组函数...

I'm trying to compare two arrays and get only the values that exist on both arrays but, unfortunately, I can't find the right array function to use...

我找到了 array_diff() 函数:http://php.net/manual/en/function.array-diff.php

但这是因为两个数组的不同.

But it's for the difference of the both arrays.

示例:

$array1 = array("**alpha**","omega","**bravo**","**charlie**","**delta**","**foxfrot**");
$array2 = array("**alpha**","gamma","**bravo**","x-ray","**charlie**","**delta**","halo","eagle","**foxfrot**");

预期输出:

$result = array("**alpha**","**bravo**","**charlie**","**delta**","**foxfrot**");

推荐答案

简单,使用 array_intersect() 代替:

Simple, use array_intersect() instead:

$result = array_intersect($array1, $array2);

这篇关于PHP比较两个数组并获得匹配的值而不是差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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