Jul*_*yag 12 php associative-array compare
我有这两个 associative arrays
//针阵列
$a = array(
"who" => "you",
"what" => "thing",
"where" => "place",
"when" => "hour"
);
Run Code Online (Sandbox Code Playgroud)
// haystack数组
$b = array(
"when" => "time",
"where" => "place",
"who" => "you",
"what" => "thing"
);
Run Code Online (Sandbox Code Playgroud)
我想,以检查是否$a有与之相匹配的b与它的准确key和value
并且如果每个键和值$a都具有精确匹配$b....我想将变量的值增加$c1,依此类推......
正如我们从上面看到的那样,有3种可能的匹配...并且据说可以将值增加$c3
$c = "3";
我希望有些天才可以帮助我......
hjp*_*r92 14
你可以看看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;
?>
Run Code Online (Sandbox Code Playgroud)
CODEPAD链接.