如何在PHP的foreach循环中获得两个项目? [英] How can i get two items in a foreach loop in PHP?
本文介绍了如何在PHP的foreach循环中获得两个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个推荐轮播.轮播每次循环通过两个项目.现在我想每次在foreach循环中得到两个项目.我怎么能得到它?
I have a testimonial carousel. The carousel loop through two item each time. Now i want to get two item each time in a foreach loop. How can i get it?
代码:
<?php foreach ($kiyra_section_meta['testimonials-group'] as $single_testimonial): ?>
<div>
<div class="row">
<div class="col-md-6">
<div class="single-review">
<div class="media">
<div class="media-body">
<h3 class="mt-0"><?php echo esc_html($single_testimonial['client-name']); ?></h3>
<h5><?php echo esc_html($single_testimonial['client-position']); ?></h5>
<i class="fas fa-quote-right fa-5x"></i>
</div>
</div>
<?php echo esc_html($single_testimonial['client-testimonial']); ?>
</div><!--/.single-review-->
</div>
</div>
</div>
<?php endforeach; ?>
推荐答案
您可以使用array_chunk将数组拆分为第2组项目,然后再次使用foreach
You can use array_chunk to split array to group 2 item, then use foreach again
foreach (array_chunk($input_array, 2) as $group) {
// Start Group
foreach ($group as $item) {
// Item
}
// End group
}
使用HTML更新
<div class="owl-carousel">
<?php foreach (array_chunk($input_array, 2) as $group) : ?>
<div class="owl-item">
<?php foreach ($group as $item) : ?>
<div class="item">
<!-- Code of item -->
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
这篇关于如何在PHP的foreach循环中获得两个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文