Break PHP array into 3 columns

2020-03-01 17:05发布

问题:

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.

回答1:

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>


回答2:

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!



回答3:

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>


回答4:

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;
    }
}


回答5:

$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.



回答6:

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>";
}


回答7:

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>';