13

我有这两个associative arrays

// 针数组

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

// 干草堆数组

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

我想检查它是否与它精确$a匹配并且bkeyvalue

如果每个键和值$a在...中都完全匹配。$b我想将变量的值增加$c1,依此类推...

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

$c = "3";

我希望有高手能帮助我...

4

1 回答 1

18

编辑2

OP 实际上用于array_intersect_assoc()他们的特定用例。(检查评论

最初的答案对他们的情况并没有真正有用!


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

编辑

这是计算匹配值的示例:

<?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;
?>

CODEPAD链接。

于 2012-04-22T07:47:19.400 回答