我有这样的代码:
<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 )
)但代码的输出只有六个链接,而不考虑条件的结果。有什么想法吗?
发布于 2013-12-30 00:08:46
这些是对象的数组-所以在数组$completedmenuitems中没有对象"5“。存在属性note = 5的对象。
你必须从数组中的对象中提取note到其他数组。
$completedmenuitems_notes = array_map(
create_function(
'$object',
'return $object->note;'
),
$completedmenuitems
);发布于 2013-12-30 00:08:31
$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))https://stackoverflow.com/questions/20827085
复制相似问题