php比较两个关联数组 [英] php compare two associative arrays
问题描述
我有这两个关联数组
//针阵列
$ a = array(
who=>you,
what=> b $ bwhere=>place,
when=>hour
);
// haystack数组
$ b = array(
when=>time,
where=>place,
who=> ;you,
what=>thing
);
我想检查 $ a
与 b
匹配键
和值
并且 $ a
中的每个键和值在 $ b
....我想增加一个变量的值 $ c
乘以1,等等...
我们从上面看到有3个可能的匹配...
并且推测会增加 $ c
由3
$ c =3;
<我希望一些天才可以帮助我...
你可以看看php的 array_diff_assoc()
函数或 array_intersect()
功能。
对匹配值进行计数的示例:
<?php
$ a = array(
who => you,
what=> thing,
where=> place,
when=> hour
);
// haystack数组
$ b = array(
when=>time,
where=>place,
who=>you,
what=>thing
);
$ c = count(array_intersect($ a,$ b));
echo $ c;
?>
CODEPAD 链接。
i have these two 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 $a
has a match with the b
with it's exact key
and value
and if each key and value from $a
has an exact match in $b
.... i want to increment the value of a variable $c
by 1 and so on...
as we've seen from above there 3 possible match...
and supposedly results to increment the value of $c
by 3
$c = "3";
i hope some genius can help me...
you can look into the php's array_diff_assoc()
function or the array_intersect()
function.
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;
?>
CODEPAD link.
这篇关于php比较两个关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!