我需要你的帮助来解决循环问题,当大脑停止运作时会很烦人。
这就是我想要做的事情:
我有一个包含大量数据的数组,在这种情况下可以说[6]
从这个数组的长度我创建了一定数量的div。每个div最多可容纳5个数组值。因此,将创建的div数量= round(sizeof array / 5),因此在这种情况下为2个div。
到目前为止一切顺利,但现在到了我无法弄清楚的部分。如何创建在div 1中打印索引0-4的循环然后前进并创建第二个div以打印输出索引5-6。
当然它应该是动态的,所以如果我在数组中有20个值,它将创建并用值填充4个div。
提前致谢!
编辑:感谢大家的解决方案!
答案 0 :(得分:1)
查看array_chuck,将数组拆分为多个部分 http://php.net/manual/en/function.array-chunk.php
// sample data
$rows = array('a','b','c','d','e','f');
$chunks = array_chunk($rows, 3); // ajust to suit number of rows per chunk
foreach ($chunks as $chunk):
echo '<div>';
foreach ($chunk as $row):
echo $row;
endforeach;
echo '</div>';
endforeach;
答案 1 :(得分:0)
伪代码:
<div>
注意:用于确定<div>
是否已打开以检查何时关闭的标志通常很有帮助。在第2步和循环之后执行此操作。
答案 2 :(得分:0)
警告:未经测试的代码。试试这个。
$perRow = 4;
$myContentArray = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$total = count($myContentArray);
$maxDivs = 3;
for($i = 0, $c = 0; $i < $total && $c <= $maxDivs; $i += $perRow + 1, $c++)
{
echo '<div>';
for($j = $i; $j <= $i + $perRow && $j < $total; $j++)
{
echo $myContentArray[$j] . ' ';
}
echo '</div>';
}
打印出来:
<div>1 2 3 4 5 </div><div>6 7 8 9 10 </div>
答案 3 :(得分:0)
使用模数运算符(%
)检查数字是否可以被另一个数字整除。在这种情况下,$x % 5 == 0
表示if the remainder of $x divided by 5 is equal to 0
。
<?php
$data = array("1","2","3","4","5","6","7","8","9","egg");
echo "<div>";
for($x=0; $x<count($data); $x++) {
echo $data[$x];
if ($x > 0 && $x % 5 == 0) {
echo "</div><div>";
}
}
echo "</div>";
?>
编辑:在回复您的评论时,我应该这样写:
<?php
$data = array("1","2","3","4","5","6","7","8","9","egg");
echo "<div>";
for($x=1; $x<=count($data); $x++) {
echo $data[$x-1];
if ($x % 5 == 0) {
echo "</div><div>";
}
}
echo "</div>";
?>
注意for
循环声明的更改,我访问$data
数组元素的方式的更改,以及对循环中条件的修改。
答案 4 :(得分:0)
<?php
$data = array(1, 2, 3, 4, 5, 6);
echo '<div>';
for($i=0;$i<count($data);$i++) {
if ($i && $i % 5 == 0) {
echo '</div><div>';
}
echo "$data[$i] ";
}
echo '</div>';
?>
答案 5 :(得分:0)
最快最简单的解决方案
$array = Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
for($i = 0 ; $i < count($array) ; $i += 5) {
echo "<div>".join(" | ", array_slice($array, $i, 5))."</div>";
}
循环:从数组中取5个元素,放入div,然后用'|连接它们“
答案 6 :(得分:0)
已有多种解决方案。我的测试和工作是使用一个容器为数据的每5个元素:
<?php
// any iterable data, not necessarily array
$data = array('a','b','c','d','e','f','g','h','i','j','k','l','m');
$counter = 0;
foreach ($data as $data_part) {
if ($counter % 5 === 0) {
echo '<div>'; // open container (div?) here
}
echo '(' . $counter . ':' . $data_part . ')';
if ($counter % 5 === 4) {
echo '</div>' . PHP_EOL; // close container (div?) here
}
$counter++;
}
if ($counter % 5 !== 4) {
echo '</div>' . PHP_EOL; // close container (div) here, because unclosed
}
要查看它的实际效果(<div>
和</div>
分别替换为[
和]
),请点击链接:http://codepad.org/GjMhiX5y
答案 7 :(得分:-2)
换句话说,你想将一维数组转换为二维数组,如下所示:
$array_one = array("A", "B", "C", "D", "E", "F", "G", "I");
$counter1 = 0;
$counter2 = 0;
foreach($array_one as $e) {
$array_two[$couter1][$couter2] = $e;
if($counter2==4) {
$counter1++;
$counter2 = 0;
}
else {
$counter2++;
}
}
或更容易:
for($i = 0; $i<count($array_one); $i++)
$array_two[$i/5][$i%5] = $array_one[$i];