我有这段代码:
<ul style="list-style-type:none;">
<?php foreach ($rawmenuitems as $rawmenuitem)
{
if (in_array($rawmenuitem->note, $completedmenuitems))
{ ?>
<li>
<span style="color:#666666; "><?php echo ($rawmenuitem->title); ?></span>
</li>
<?php }
else
{ ?>
<li>
<?php echo ('<a href =' . $rawmenuitem->link . '&Itemid=' . $rawmenuitem->id . '">' . $rawmenuitem->title . '</a>'); ?>
</li>
<?php }
}?>
</ul>
数组是:
$rawmenuitems (
[0] => stdClass Object ( [link] => index.php?option=com_breezingforms&view=form [id] => 1378 [title] => 334 Basic Information [note] => 5 )
[1] => stdClass Object ( [link] => index.php?option=com_breezingforms&view=form [id] => 1381 [title] => 334 Drug Testing [note] => 17 )
[2] => stdClass Object ( [link] => index.php?option=com_breezingforms&view=form [id] => 1380 [title] => 334 Emergency Treatment [note] => 15 )
[3] => stdClass Object ( [link] => index.php?option=com_breezingforms&view=form [id] => 1379 [title] => 334 Extracurricular [note] => 7 )
[4] => stdClass Object ( [link] => index.php?option=com_breezingforms&view=form [id] => 1377 [title] => 334 Florida Concussion [note] => 12 )
[5] => stdClass Object ( [link] => index.php?option=com_breezingforms&view=form [id] => 1376 [title] => 334 Florida Consent [note] => 14 )
)
和
$completedmenuitems (
[0] => stdClass Object ( [id] => 1377 [note] => 12 )
[1] => stdClass Object ( [id] => 1376 [note] => 14 )
)
但是代码的输出只是六个链接,而不考虑条件的结果。有什么想法吗?
答案 0 :(得分:3)
那些是对象数组 - 所以在数组$ completedmenuitems中没有对象“5”。有一个对象有属性note = 5.
您必须从对象中将note
从数组中提取到其他数组。
$completedmenuitems_notes = array_map(
create_function(
'$object',
'return $object->note;'
),
$completedmenuitems
);
答案 1 :(得分:0)
$rawmenuitem->note
是一个整数,而$completedmenuitems
是一个包含名为note
的属性的对象数组。
因此,in_array()
将整数与对象进行比较。
这个问题有多种解决方案。一个是推出自己的功能:
function isCompletedItem($completedItems, $note) {
foreach ($completedItems => $items) {
if ($items->note == $note) {
return true;
}
}
return false;
}
if (in_array($completedmenuitems, $rawmenuitem->note))