我有这两个
associative arrays
针阵列
$a = array(
"who" => "you",
"what" => "thing",
"where" => "place",
"when" => "hour"
);
干草堆阵列
$b = array(
"when" => "time",
"where" => "place",
"who" => "you",
"what" => "thing"
);
我想检查
$a
是否与 b
及其确切的 key
和 value
匹配
并且如果
$a
中的每个键和值在 $b
中都完全匹配......我想将变量 $c
的值增加 1 等等...
正如我们从上面看到的,有 3 种可能的匹配...... 据称结果是将
$c
的值增加 3
$c = "3";
array_intersect_assoc()
用于其特定用例。(检查评论)
原来的答案对他们的案例来说并没有多大用处!
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;
?>
键盘链接。