从一个数组中获得所有值,而另一个数组中不存在该值 [英] To get all value from one array which is not exists in another array

查看:106
本文介绍了从一个数组中获得所有值,而另一个数组中不存在该值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从不在PHP的另一个数组中的数组中获取值?

How to get the values from an array that are NOT in another array in PHP?

我目前的做法有很差的时间复杂度.有内置的php函数可以解决我的问题吗?

My current aproach have bad time complexity. Is there an inbuilt php function that can solve my problem?

示例:

$a1 = array(1,2,3,4);
$a2 = array(3,4,5,6,7);

结果:

[5,6,7];

推荐答案

array_diff 是你的朋友.

返回一个数组,该数组包含array1中所有其他数组中不存在的所有条目.

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

$a1 = array(1,2,3,4);
$a2 = array(3,4,5,6,7);
$result = array_diff($a2, $a1);   
print_r($result);
Array
(
    [2] => 5
    [3] => 6
    [4] => 7
)

这篇关于从一个数组中获得所有值,而另一个数组中不存在该值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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