【问题标题】:PHP use in_array to check array against another arrayPHP 使用 in_array 来检查另一个数组的数组
【发布时间】:2019-05-16 15:42:27
【问题描述】:

我正在尝试检查 PHP 中的数组 $a2 中是否存在任何值 out 或数组 $a1...

$a1 = array(
    "a"=>"red",
    "b"=>"green",
    "c"=>"blue",
    "d"=>"yellow"
);

$a2 = array(
    "b"=>"green",
    "c"=>"blue",
);

我曾尝试像这样使用in_array 进行比较...

if (in_array($a1, $a2)) {
  echo "Match found";
}

但这不起作用,我认为这是因为 in_array 不支持根据数组检查数组。正确的方法是什么?

【问题讨论】:

  • 你试过array_diff()

标签: php


【解决方案1】:

你可以使用array_intersect_assoc

$res =array_intersect_assoc($a1, $a2);

Live Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多