我有以下数据结构:
Array
(
[0] => stdClass Object
(
[id] => 1
[title] => Golf
[manufacturer] => 1
)
[1] => stdClass Object
(
[id] => 2
[title] => Focus
[manufacturer] => 2
)
[2] => stdClass Object
(
[id] => 3
[title] => Jazz
[manufacturer] =>
)
[3] => stdClass Object
(
[id] => 4
[title] => Passat
[manufacturer] => 1
)
[4] => stdClass Object
(
[id] => 5
[title] => Toureg
[manufacturer] => 1
)
[5] => stdClass Object
(
[id] => 6
[title] => Galaxy
[manufacturer] => 2
)
)
我想这样显示它:
<div class="container id1">
ID: 1 - Golf
ID: 4 - Passat
ID: 5 - Toureg
</div>
<div class="container id2">
ID: 2 - Focus
ID: 6 - Galaxy
</div>
<div class="container">
ID: 3 - Jazz
</div>
所以我的视图代码将输出我尝试这样做的上述数组:
foreach ($cars as $car){
echo '<div class="container">';
if ($car->manufacturer) {
echo '</div>';
echo '<div class="container id'. $car->manufacturer .'">';
echo $car->id . ' - ' . $car->title;
echo '</div>';
} else {
echo '</div>';
echo '<div class="container">';
echo $car->id . ' - ' . $car->title;
echo '</div>';
}
}
但这会输出几个空<div class="container"></div>元素,而且只有<div class="container">ID: 1 - Golf</div>, 不像所需的输出。如何使用此数组获得所需的输出?请注意,Jazz 没有制造商集,因此<div>类不应该有 idX 集,其中 X 是制造商的 ID 值。
慕桂英3389331