gpt4 book ai didi

php - 每 4 次迭代创建行并关闭 div

转载 作者:太空宇宙 更新时间:2023-11-04 01:00:35 25 4
gpt4 key购买 nike

假设这是分配给 $my_array 变量的我的数据数组。

[0] => Array
(
[0] => 0 seconds
[1] => 05:00 PM to 6:00 PM
)

[1] => Array
(
[0] => 6 minutes
[1] => 08:00 AM to 9:00 AM
)

[2] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 08
[1] => 15 AM
)

)

[3] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 08
[1] => 30 AM
)

)

[4] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 08
[1] => 45 AM
)

)

[5] => Array
(
[0] => 15 minutes
[1] => 09:00 AM to 10:00 AM
)

[6] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 09
[1] => 15 AM
)

)

[7] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 09
[1] => 30 AM
)

)

[8] => Array
(
[0] => 15 minutes
[1] => Array
(
[0] => 09
[1] => 45 AM
)

)

[9] => Array
(
[0] => 6 minutes
[1] => 10:00 AM to 11:00 AM
)

[10] => Array
(
[0] => 6 minutes
[1] => Array
(
[0] => 12
[1] => 30 PM
)

)

)

我有这样的东西:

foreach($my_array as $key => $a) {
if(!is_array($a[1)) { ?>
<div><?php echo $a; ?></div>
<?php if($key % 4 == 0) { ?>
<div class="row">
<?php } ?>
<?php } ?>

<div class="col-md-3">
<?php echo $a[0]; ?>
</div>

<?php if(!is_array($a[1)) { ?>
<?php if($key % 4 == 0) { ?>
</div>
<?php }
} ?>
}

如果第二个数组元素不是数组“if(!is_array($a[1))”并且在 4 次迭代后关闭 div,我想要实现的是创建一个包含 4 列的新行。

为了清楚起见,我根据上面给出的数据创建了一个我想要实现的静态 html:

输出应该是这样的:

enter image description here

最佳答案

我对所有那些打开和关闭 PHP 标记做了一点清理,并选择回显 HTML 代码位。很好地清除了一些语法错误。

foreach($my_array as $key => $a) {

if(!is_array($a[1])) {
echo "<div>" . $a[1] . "</div>\n";
if($key % 4 == 0) {
echo "<div class=\"row\">\n";
}
}

// create a new row div before the fourth divisible index.
// this was necessary to insert the div below the timeblock div ($a[1])
if((($key - 1) % 4) == 0) {
echo "<div class=\"row\">\n";
}

echo "\t<div class=\"col-md-3\">" . $a[0] . "</div>\n";

// added condition to close the row div on the last item
if(($key % 4 == 0) || ($key == (count($my_array) -1))) {
echo "</div>\n";
}
}

关于php - 每 4 次迭代创建行并关闭 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53476237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com