我有這個問題關於在foreach中的if/elseif條件。我一遍又一遍地搜索,真的沒有想法。 所以我有此數組:Php elseif條件在foreach
陣列 (
[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已標題=>主obj2的具有標題=>二, 「標題」,其餘被設置爲其他。
我有這樣的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;
這個循環returnes兩個 「跨越」 和 「格」,我不想要的。如果「second」不存在,我無法只顯示title =>「second」(如果它存在),title =>「main」。所以要麼顯示跨度,要麼div的。
希望我解釋好,如果不是我道歉!非常感謝你,我非常感謝你的幫助!
馬呂斯
及後'$秒= TRUE;'你可以使用'突破;',因爲那時它不如果發生「main」,這很重要。 – 2012-01-08 23:42:06
@PeterKrejci好點,編輯 – DaveRandom 2012-01-08 23:43:36
Briliant!非常感謝你們!! – MariusH 2012-01-08 23:52:57