php php比较两个关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10266148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:46:44  来源:igfitidea点击:

php compare two associative arrays

phpcompareassociative-array

提问by Julian Paolo Dayag

i have these two associative arrays

我有这两个 associative arrays

// the needle array

// 针数组

$a = array(
"who" => "you", 
"what" => "thing", 
"where" => "place",
"when" => "hour"
);

// the haystack array

// 干草堆数组

$b = array(
"when" => "time", 
"where" => "place", 
"who" => "you",
"what" => "thing"
);

i want to check if the $ahas a match with the bwith it's exact keyand value

我想检查它是否与它完全$a匹配并且bkeyvalue

and if each key and value from $ahas an exact match in $b.... i want to increment the value of a variable $cby 1 and so on...

并且如果来自的每个键和值$a$b.... 中具有完全匹配,我想将变量的值增加$c1 等等...

as we've seen from above there 3 possible match... and supposedly results to increment the value of $cby 3

正如我们从上面看到的那样,有 3 个可能的匹配项......并且据说结果将 的值增加了$c3

$c = "3";

$c = "3";

i hope some genius can help me...

我希望有天才可以帮助我...

回答by hjpotter92

you can look into the php's array_diff_assoc()function or the array_intersect()function.

你可以查看php的array_diff_assoc()函数或者array_intersect()函数。

EDIT

编辑

Here's a sample on counting the matched values:

以下是计算匹配值的示例:

<?php
  $a = array(
    "who" => "you", 
    "what" => "thing", 
    "where" => "place",
    "when" => "hour"
  );
  // the haystack array
  $b = array(
    "when" => "time", 
    "where" => "place", 
    "who" => "you",
    "what" => "thing"
  );
  $c = count(array_intersect($a, $b));
  echo $c;
?>

CODEPADlink.

键盘链接。