我有以下数组名为investmentprogramscriteria和companyInvestmentProfil:
Array
(
[0] => Array
(
[criteriaID] => 55
[criteriaIsMatchedIf] => 11, 12, 13
)
[1] => Array
(
[criteriaID] => 54
[criteriaIsMatchedIf] => 1
)
[2] => Array
(
[criteriaID] => 52
[criteriaIsMatchedIf] => 1
)
)
Array
(
[0] => Array
(
[criteriaID] => 52
[investmentprofileCriteriaAnswer] => 1
)
[1] => Array
(
[criteriaID] => 54
[investmentprofileCriteriaAnswer] => 1
)
[2] => Array
(
[criteriaID] => 58
[investmentprofileCriteriaAnswer] => 0
)
[3] => Array
(
[criteriaID] => 59
[investmentprofileCriteriaAnswer] => 1
)
[4] => Array
(
[criteriaID] => 55
[investmentprofileCriteriaAnswer] => 1
)
)
我试图找出第一个数组(investmentprogramscriteria)中的criteriaID的值是否存在于第二个数组(companyInvestmentProfil)中,并且IF第一个数组中的key criteriaIsMatchedIf的值是否等于键的值来自第二个阵列的investmentprofileCriteriaAnswer。
我当前的php代码此时返回错误的结果:
if (array_intersect($investmentprogramscriteria,$companyInvestmentProfil)) {
echo "Match";
} else {
echo "Not match";
}
答案 0 :(得分:1)
array_column()
可以提取指示为单个维度的列,并按指示的下一列对其进行索引。对两个数组执行此操作,然后使用array_intersect_assoc()
检查键和值:
if(array_intersect_assoc(
array_column($investmentprogramscriteria, 'criteriaIsMatchedIf', 'criteriaID'),
array_column($companyInvestmentProfil, 'investmentprofileCriteriaAnswer', 'criteriaID'))) {
echo "Match";
} else {
echo "No Match";
}
{p <1}}需要PHP&gt; = 5.5.0或使用PHP Implementation of array_column()。
答案 1 :(得分:1)
您可以使用array_map尝试代码:
$elements1 = array();
foreach($investmentprogramscriteria as $item) {
$elements1[] = $item['criteriaID'] . $item['criteriaIsMatchedIf'];
}
$elements2 = array();
foreach($companyInvestmentProfil as $item) {
$elements2[] = $item['criteriaID'] . $item['criteriaIsMatchedIf'];
}
if (array_intersect($elements1, $elements2)) {
echo "Match";
} else {
echo "Not match";
}