我正在尝试创建一个简单的foreach循环,它将对数组中的每16条记录进行分组。我还有另一个计数器($ s),我需要在4点后重置。
这是我的代码
$a = array(
'book-1' => 'Book Title 1',
'book-2' => 'Book Title 2',
'book-3' => 'Book Title 3',
'book-4' => 'Book Title 4',
'book-5' => 'Book Title 5',
'book-6' => 'Book Title 6',
'book-7' => 'Book Title 7',
'book-8' => 'Book Title 8',
'book-9' => 'Book Title 9'
);
$i = 1;
$bookshelf = 1;
$s=0;
foreach ( $a as $k => $v )
{
if ($i == 9 || $i == 1)
{
echo '<div id="bookshelf-'.$bookshelf.'"></div>';
$bookshelf++;
}
if ($i%4 == 1)
{
echo '<div class="bookshelf-bottom">';
}
if ($i > 5)
{
$s=0;
}
$s++;
echo '<div class="book-frame-'.$s.'">'.$v.'</div>';
if ($i%4 == 0)
{
echo '</div>';
}
$i++;
}
if ($i%4 != 1) echo '</div>';
我想要导出的是:
<div id="bookshelf-1" class="bookshelf-top"></div>
<div class="bookshelf-bottom">
<div class="book-frame-1">Book Title 1</div>
<div class="book-frame-2">Book Title 2</div>
<div class="book-frame-3">Book Title 3</div>
<div class="book-frame-4">Book Title 4</div>
</div>
<div class="bookshelf-bottom">
<div class="book-frame-1">Book Title 5</div>
<div class="book-frame-2">Book Title 6</div>
<div class="book-frame-3">Book Title 7</div>
<div class="book-frame-4">Book Title 8</div>
</div>
<div class="bookshelf-bottom">
<div class="book-frame-1">Book Title 9</div>
</div>
答案 0 :(得分:1)
看看
<?php
$a = array(
'book-1' => 'Book Title 1',
'book-2' => 'Book Title 2',
'book-3' => 'Book Title 3',
'book-4' => 'Book Title 4',
'book-5' => 'Book Title 5',
'book-6' => 'Book Title 6',
'book-7' => 'Book Title 7',
'book-8' => 'Book Title 8',
'book-9' => 'Book Title 9'
);
$i = 1;
$bookshelf = 1;
$s=0;
foreach ( $a as $k => $v ) {
$s++;
if ($i == 9 || $i == 1) {
if($i == 1)
echo '<div id="bookshelf-'.$bookshelf.'" class="bookshelf-top"></div>';
else
echo '<div id="bookshelf-'.$bookshelf.'"></div>';
$bookshelf++;
}
if($s == 1)
echo '<div class="bookshelf-bottom">';
echo '<div class="book-frame-'.$s.'">'.$v.'</div>';
if($s == 4) {
echo '</div>';
$s = 0;
}
$i++;
}
?>
答案 1 :(得分:0)
如果代码变得更复杂,那么你将无限地理解这种做法。相反,我会选择这样的事情:
chartWrapper.draw(); // initial draw of the chart
var chart = chartWrapper.getChart();
var cli = chart.getChartLayoutInterface();
var bb = cli.getChartAreaBoundingBox();
var vAxisMax = cli.getVAxisValue(bb.top);
这使用array_chunk创建你的部分,然后迭代这些部分来制作html。
编辑:如果您想要包含一个包含更多值的完整数组以及您需要生成的更多HTML,我们很乐意修改此foreach并解释它是如何工作的。< / p>