在foreach循环中分别显示奇数和偶数数组数据
Separately display Odd and Even array data in a foreach loop
<?php $ctr = 0; ?>
@foreach($level1['Weapons'] as $key => $header)
@if($ctr% == 0)
<div class="left">
@endif
@if($ctr% == 1)
<div class="right">
@endif
<ul>
<li>
<span class="ability">{{$key}}</span><br>
<p class="ability-desc">{{$header['desc']}}</p>
</li>
</ul>
@if($ctr == 8 || $ctr == 7)
</div>
@endif
<?php $ctr++; ?>
@endforeach
数组:
'level1' =>
array (
'Weapons' =>
array (
'Misfortune' =>
array (
'desc' => 'You can make any person fall',
),
'Sorrow' =>
array (
'desc' => 'You can make a person cry just by looking at them',
),
'Stink' =>
array (
'desc' => 'No living person can endure your fart',
),
'Harden' =>
array (
'desc' => 'You can make soft things hard, even water',
),
我有一个 foreach,它将列出数组描述中的所有项目。我的问题是我创建了一个 if 语句来将 ODD 和 EVEN 数组元素的值分配给 div ,这将根据结果将它们分成左右。我注意到结果似乎只显示在左侧。我如何重新安排或制作满足我的想法的 if 语句。我希望这个问题是有道理的。
我会在你的循环中构建 2 个字符串,然后在你的循环之后将它们回显出来。我做 php 已经有一段时间了,所以可能会有一些语法错误,但您应该了解总体思路:
<?php
$ctr = 0;
$leftLi = "";
$rightLi = "";
foreach($level1['Weapons'] as $key => $header)
{
if ($ctr % 2 == 0)
{
$leftLi += MakeLi($key, $header['desc']);
} else {
$rightLi += MakeLi($key, $header['desc']);
}
$ctr++;
}
function MakeLi($key, $headerText)
{
return "
<li>
<span class=\"ability\">" . $key . "</span><br>
<p class=\"ability-desc\">" . $headerText . "</p>
</li>";
}
?>
<div class="left">
<ul>
<?php echo $leftLi; ?>
</ul>
</div>
<div class="right">
<ul>
<?php echo $rightLi; ?>
</ul>
</div>
<?php $ctr = 0; ?>
@foreach($level1['Weapons'] as $key => $header)
@if($ctr% == 0)
<div class="left">
@endif
@if($ctr% == 1)
<div class="right">
@endif
<ul>
<li>
<span class="ability">{{$key}}</span><br>
<p class="ability-desc">{{$header['desc']}}</p>
</li>
</ul>
@if($ctr == 8 || $ctr == 7)
</div>
@endif
<?php $ctr++; ?>
@endforeach
数组:
'level1' =>
array (
'Weapons' =>
array (
'Misfortune' =>
array (
'desc' => 'You can make any person fall',
),
'Sorrow' =>
array (
'desc' => 'You can make a person cry just by looking at them',
),
'Stink' =>
array (
'desc' => 'No living person can endure your fart',
),
'Harden' =>
array (
'desc' => 'You can make soft things hard, even water',
),
我有一个 foreach,它将列出数组描述中的所有项目。我的问题是我创建了一个 if 语句来将 ODD 和 EVEN 数组元素的值分配给 div ,这将根据结果将它们分成左右。我注意到结果似乎只显示在左侧。我如何重新安排或制作满足我的想法的 if 语句。我希望这个问题是有道理的。
我会在你的循环中构建 2 个字符串,然后在你的循环之后将它们回显出来。我做 php 已经有一段时间了,所以可能会有一些语法错误,但您应该了解总体思路:
<?php
$ctr = 0;
$leftLi = "";
$rightLi = "";
foreach($level1['Weapons'] as $key => $header)
{
if ($ctr % 2 == 0)
{
$leftLi += MakeLi($key, $header['desc']);
} else {
$rightLi += MakeLi($key, $header['desc']);
}
$ctr++;
}
function MakeLi($key, $headerText)
{
return "
<li>
<span class=\"ability\">" . $key . "</span><br>
<p class=\"ability-desc\">" . $headerText . "</p>
</li>";
}
?>
<div class="left">
<ul>
<?php echo $leftLi; ?>
</ul>
</div>
<div class="right">
<ul>
<?php echo $rightLi; ?>
</ul>
</div>