我有以下数组名为investmentprogramscriteria和companyInvestmentProfil:
Array
(
[0] => Array
(
[investmentprogramcriteriaID] => 20
[investmentprogramID] => 21
[criteriaID] => 59
[criteriaIsMatchedIf] => 1
)
[1] => Array
(
[investmentprogramcriteriaID] => 21
[investmentprogramID] => 21
[criteriaID] => 57
[criteriaIsMatchedIf] => 1
)
)
Array
(
[0] => Array
(
[investmentprofileID] => 1
[companyID] => 27
[criteriaID] => 54
[investmentprofileCriteriaAnswer] => 1
)
[1] => Array
(
[investmentprofileID] => 2
[companyID] => 27
[criteriaID] => 57
[investmentprofileCriteriaAnswer] => 0
)
[2] => Array
(
[investmentprofileID] => 3
[companyID] => 27
[criteriaID] => 58
[investmentprofileCriteriaAnswer] => 0
)
[3] => Array
(
[investmentprofileID] => 4
[companyID] => 27
[criteriaID] => 59
[investmentprofileCriteriaAnswer] => 1
)
)
如果在第二个数组中找到第一个数组中的最后两个键及其值,则代码应返回匹配, 否则应该返回不匹配。
我尝试用数组函数解决这个问题,但没有任何效果,现在我试图用php解决它,但到目前为止没有运气。
我正在尝试:
foreach ($investmentprogramscriteria as $programcriteria) {
//foreach($companyInvestmentProfil as $profile) {
if (($programcriteria['criteriaID'] == $companyInvestmentProfil[$i]['criteriaID']) && ($programcriteria['criteriaIsMatchedIf'] == $companyInvestmentProfil[$i]['investmentprofileCriteriaAnswer'])) {
$match = "Match";
} else {
$match = "Not Match";
continue;
// }
}
}
答案 0 :(得分:2)
<强>更新强>
您的代码包含错误:
$programcriteria['criteriaID'] == $companyInvestmentProfil[$i]['criteriaID']
永远不会定义变量$i
。你可能想写一些类似的东西:
// assign keys to $i
foreach ($investmentprogramscriteria as $i => $programcriteria) {
}
在这种情况下,如果您在array_slice
和{array_keys
上使用array_intersect_assoc
和$programcriteria
,请使用下面的$companyInvestmentProfil[$k]
+ $intersect = array_intersect_assoc($array1, $array2);
方法。分别为{1}}数组。
您基本上寻找的是the array_intersect_assoc
function。该函数返回一个数组,其中包含两个数组中存在的所有键值对:
count($intersect);
要查看有多少个键值对匹配,只需$lastKeys = array_slice(array_keys($array1), -2);
if (isset($intersect[$lastKeys[0]]) && isset($intersect[$lastKeys[1])) {
return 'match';
}
return 'no match';
要查看第一个数组的最后两个键是否匹配,只需写下:
<WrapPanel Height="200" VerticalAlignment="Top" Orientation="Vertical">
<Button x:Name="BookCover" Click="ChangeImageFile" Margin="10">
button style and content
</Button>
<WrapPanel Margin="10">
<TextBox Text="New Book"></TextBox>
<TextBox Text="Lorem ipsum dolor sit amet, eos iusto graece consetetur cu, an dicam nonumy volutpat qui, usu at nullam alienum. Est suas autem augue ad, integre vituperatoribus quo id, delicata gloriatur pro ei. Vel at nisl decore, ut eum altera commune. Nec at agam menandri, putant insolens suavitate ei mei, ea possim veritus mea. Nec nemore iuvaret detracto at, quodsi consectetuer pri id. Integre suscipit voluptua no pro, nonumes incorrupte ut mea. Delenit democritum vel in, quem tale everti vim id, id vis melius necessitatibus. Paulo signiferumque per te. Cu mei modus exerci partiendo, ad iudico adolescens mea, et stet integre civibus mea." TextWrapping="Wrap"></TextBox>
</WrapPanel>
</WrapPanel>
查看手册on array_slice
了解详情。
答案 1 :(得分:1)
最好的方法是使用foreach两次!!!
foreach ($investmentprogramscriteria as $value) {
foreach ($companyInvestmentProfil as $v) {
if($v["criteriaID"] == $value["criteriaID"]) echo "match<br>";
else echo "not Match<br>";
}
}
答案 2 :(得分:0)
$array1 = array("a" => "greens", "reds", "blues", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
if(is_array($result)){
echo 'match';
}else{
echo 'not match';
}