将 PHP 数组分成 3 列

发布于 2024-12-23 11:44:02 字数 268 浏览 2 评论 0原文

我正在尝试将 PHP 数组分成 3 列(必须是列,而不是行),因此它看起来像这样:

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

我能想到的最佳方法是将主数组分成 3 个数组,每个数组 1 个尽管我无法找出执行此操作的最佳方法 - 更具体地说是我可以用来生成 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................

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

忆沫 2024-12-30 11:44:02

我会使用这个:

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

或者使用 html 表时:

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

I would use this:

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

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>
删除→记忆 2024-12-30 11:44:02

您可以使用 array_slice 提取数组的一部分,因此:

$newArray = array();

for($i = 0; $i < count($oldArray); $i += 3) {
    $newArray[] = array_slice($oldArray, $i, 3);
}

编辑:As @ deceze 指出,这与 array_chunk 执行相同的操作。 (我知道 PHP 会有内置的东西。)所以请使用它!

You can use array_slice to extract a section of an array, so:

$newArray = array();

for($i = 0; $i < count($oldArray); $i += 3) {
    $newArray[] = array_slice($oldArray, $i, 3);
}

Edit: As @deceze points out, this does the same thing as array_chunk. (I knew PHP would have something built-in.) So use that instead!

梦亿 2024-12-30 11:44:02
$data = array(); 
$columns = 3;

echo "<table><tr>";

for($i = 0; $i < count($data); $i++)
{
 if($i%$columns == 0)
  echo "</tr><tr>";
 echo "<td>".$data[i$]."</td>";
}
echo "</tr></table>";

就这样,当您达到列数时,它只会输出另一行。当数据不是 3 的倍数时,您可能需要添加一些逻辑。

$data = array(); 
$columns = 3;

echo "<table><tr>";

for($i = 0; $i < count($data); $i++)
{
 if($i%$columns == 0)
  echo "</tr><tr>";
 echo "<td>".$data[i$]."</td>";
}
echo "</tr></table>";

There you go, it just outputs another row when you get to your column count. You might need to add some logic when the data isnt a multiple of 3.

冷情妓 2024-12-30 11:44:02

使用 array_chunk 你可以分割数组:

$rows = array_chunk($yourArray, '3'); // 3 = column count;
foreach ($rows as $columns) {
    echo "<div class='row'>";
    foreach ($columns as $column) { 
        echo "<div class='column'>$column</div>"; 
    }
    echo "</div>";
}

Using array_chunk you can split array :

$rows = array_chunk($yourArray, '3'); // 3 = column count;
foreach ($rows as $columns) {
    echo "<div class='row'>";
    foreach ($columns as $column) { 
        echo "<div class='column'>$column</div>"; 
    }
    echo "</div>";
}
紅太極 2024-12-30 11:44:02

在考虑您的问题主题时,第一印象是“专栏”,我认为您需要关注视觉方面。 “如何将数组显示为三列”可能是我的想法是错误的。但我想你想要那样。只要检查下面的例子我的想法是否正确。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>
<?php
$arr = array(1,2,3,4,5,6,7,8,9,10);
?>
<body>
<div style="width:300px">
<ul>
    <?php foreach($arr as $itm) {?>
    <li style="list-style:none;float:left;width:80px;"><span style="font-size:36px;"><?php echo $itm;?></span></li>
    <?php }?>
</ul>
</div>
</body>
</html>

When considering your question topic it's first impression is"Column" and I think you needed to focus on visual aspects. "How to display your array as three columns" May be my idea is wrong. But I think you wanted that. Just check following example if my thought is correct.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>
<?php
$arr = array(1,2,3,4,5,6,7,8,9,10);
?>
<body>
<div style="width:300px">
<ul>
    <?php foreach($arr as $itm) {?>
    <li style="list-style:none;float:left;width:80px;"><span style="font-size:36px;"><?php echo $itm;?></span></li>
    <?php }?>
</ul>
</div>
</body>
</html>
宛菡 2024-12-30 11:44:02

刚刚想了一下,这应该可以实现我想要的 - 尽管它是否是最快的方法,但我不确定:

$array = (1,2,3,4,5,6,7,8,9....)


$column1 = array();
    $i = 2;

foreach($array as $value) {
    if ($i++ % 3 == 0) {
        $column1[] = $value;
    }
}


$column2 = array();
    $i = 1;

foreach($array as $value) {
    if ($i++ % 3 == 0) {
        $column2[] = $value;
    }
}

$column3 = array();
    $i = 0;

foreach($array as $value) {
    if ($i++ % 3 == 0) {
        $column3[] = $value;
    }
}

编辑:尽管使用单个 foreach 循环,但与上面相同:

$array = (1,2,3,4,5,6,7,8,9....)


    $column1 = array();
    $column2 = array();
    $column3 = array();
    $i = 2;
    $j = 1;
    $k = 0;


foreach($array as $value) {
    if ($i++ % 3 == 0) {
        $column1[] = $value;
    }
    if ($j++ % 3 == 0) {
        $column2[] = $value;
    }
    if ($k++ % 3 == 0) {
        $column3[] = $value;
    }
}

Having just thought about it, this should achieve what I want - whether it's the fastest method though, I'm not sure:

$array = (1,2,3,4,5,6,7,8,9....)


$column1 = array();
    $i = 2;

foreach($array as $value) {
    if ($i++ % 3 == 0) {
        $column1[] = $value;
    }
}


$column2 = array();
    $i = 1;

foreach($array as $value) {
    if ($i++ % 3 == 0) {
        $column2[] = $value;
    }
}

$column3 = array();
    $i = 0;

foreach($array as $value) {
    if ($i++ % 3 == 0) {
        $column3[] = $value;
    }
}

EDIT: The same as above although using a single foreach loop:

$array = (1,2,3,4,5,6,7,8,9....)


    $column1 = array();
    $column2 = array();
    $column3 = array();
    $i = 2;
    $j = 1;
    $k = 0;


foreach($array as $value) {
    if ($i++ % 3 == 0) {
        $column1[] = $value;
    }
    if ($j++ % 3 == 0) {
        $column2[] = $value;
    }
    if ($k++ % 3 == 0) {
        $column3[] = $value;
    }
}
屋檐 2024-12-30 11:44:02
header('Content-Type: text/plain');

echo '<table>';

$colNum = 3;
$regNum = 10;

for ($i = 1; $i <= $regNum; $i++) {

    if (($i % $colNum) == 1) {

        echo "\n\t".'<tr>';

    }

    echo "\n\t\t".'<td>'. $i .'</td>';

    if (($i % $colNum) == 0 or $regNum <= $i) {

        echo "\n\t".'</tr>';

    }

}

echo "\n".'</table>';
header('Content-Type: text/plain');

echo '<table>';

$colNum = 3;
$regNum = 10;

for ($i = 1; $i <= $regNum; $i++) {

    if (($i % $colNum) == 1) {

        echo "\n\t".'<tr>';

    }

    echo "\n\t\t".'<td>'. $i .'</td>';

    if (($i % $colNum) == 0 or $regNum <= $i) {

        echo "\n\t".'</tr>';

    }

}

echo "\n".'</table>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文