我需要比较两个数组并根据结果传递变量值。 我循环$ additionalTestsAvailable,如果$ additionalTestsSelected在$ additionalTestsSelected中有一个值,将'Yes'传递给additionalTestsSelected,否则传递''。但结果都是''。它不应该这样。我的代码出了什么问题?
$additionalTestsSelected = $appointmentTime->getAdditionalTestsSelected();
$additionalTestsAvailable = $appointmentTime->getAdditionalTestsAvailable();
foreach($additionalTestsAvailable as $test) {
if(in_array($test,$additionalTestsSelected)){
$additionalTests[$test->getName()] = 'Yes';
}else{
$additionalTests[$test->getName()] = '';
}
}
答案 0 :(得分:0)
我怀疑你的测试是作为不同的对象从get函数中出来的,即使它们具有相同的名称,因此in_array
比较失败。您可以通过手动循环并使用其名称比较测试来解决此问题:
foreach ($additionalTestsAvailable as $test) {
$result = '';
foreach ($additionalTestsSelected as $k) {
if ($test->getName() == $k->getName()) {
$result = 'Yes';
break;
}
}
$additionalTests[$test->getName()] = $result;
}