【发布时间】:2012-02-05 15:07:34
【问题描述】:
我在 foreach 外观中遇到了关于 if/elseif 条件的问题。我一遍又一遍地搜索,真的没有想法。 所以我有这个数组:
数组 (
[0] => stdClass Object
(
[title] => main
[value] => value1
)
[1] => stdClass Object
(
[title] => other
[value] => value2
)
[2] => stdClass Object
(
[title] => second
[value] => value3
)
[3] => stdClass Object
(
[title] => other
[value] => value4
)
[4] => stdClass Object
(
[title] => other
[value] => value5
)
)
如您所见,obj0 有 title=>main,obj2 有 title=>second,其余的“titles”设置为 other。
我有这个 foreach:
foreach ($objects as $object):
if($object->title == 'second') {
//this value is rarely added, so it must have a "priority" and be showed
echo '<span></span>';
} elseif($object->title == 'main') {
//i mostly want to show this, but not if $object->title has the value 'second'.
echo '<div></div>';
}
endforeach;
这个循环返回我不想要的“span”和“div”。我无法仅显示 title=>"second" (如果存在),并且仅在 "second" 不存在时才显示 title=>"main" 。所以无论是显示跨度,还是 div 的。
希望我解释得很好,否则我道歉!非常感谢您,非常感谢您的帮助!
马吕斯
【问题讨论】:
标签: php arrays foreach conditional-statements if-statement