本文介绍了将PHP数组分为3列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图将一个PHP数组分为3列(必须是列,而不是行),所以它看起来像这样:

I'm trying to break a PHP array into 3 columns (has to be columns, not rows) so it would look something like this:

Item 1     Item 2     Item 3
Item 4     Item 5     Item 6
Item 7     Item 8     Item 9
Item 10................

我能想到的最佳方法是将主数组分成3个数组,每列1个,尽管我无法找出最佳方法来做到这一点-更具体地说,我可以使用该标准来生成3个数组数组.

The best approach I can think of would be to break the main array into 3 arrays, 1 for each column although I can't work out the best approach to do this - more specifically the criteria I could use to generate the 3 arrays.

推荐答案

我会用这个:

$i = 1
foreach ($array as $value) {
  if ($i % 3 === 0) {
        echo $value . '<br />';
    }
   $i++;
}

或者使用html表时:

Or when using html table:

<table>
<tr>
   <?php
   $i = 0;
   foreach ($array as $value) {
      if ($i % 3 === 0) {
            echo '</tr><tr>';
        }
      echo "<td>" . $value . "</td>";
      $i++;
   }
   ?>
</tr>
</table>

这篇关于将PHP数组分为3列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 05:53
查看更多