4

I have an interesting problem, seems like it would have been solved long ago but can't find anything.

I have a simple array, variable length.

I need to put the array in a table BUT only X columns wide. Meaning, if the array has 25 values, and I only want 3 columns, there will be 2 columns of 11 and 1 column of 3 (the remainder).

Like this (each number represents a value in a table cell):

1    12    23
2    13    24
3    14    25
4    15
5    16
6    17
7    18
8    19
9    20
10   21
11   22

I've spent all day afternoon this, seems like it would be simple, but either my game is off today or it's harder than I think.

Of course, no problem going horizontally, easy, but vertically is the requirement and I never know the size of the array and number of columns can vary.

Thanks for any help!

4
  • 3
    Why not 2 columns of 10 and the third of 5 ? Commented Oct 11, 2016 at 22:15
  • 1
    I can imagine this to be a bit tricky simply because, as long as the left columns are equal in size and are longer than the right-most column, that would be a valid solution. That is to say, two columns of 10 and one of 5 would fit the requirements, two columns of 12 and one of 1 would fit, two columns of 9 and one of 7 would fit... Commented Oct 11, 2016 at 22:15
  • so is the number of columns and the length of a column an argument for a function that should "print" it? Commented Oct 11, 2016 at 22:16
  • 3
    What is the rule how many items go into one column? What's the maximum column count that is allowed? This looks like a simple problem you can solve in one foreach loop ;) Commented Oct 11, 2016 at 22:18

3 Answers 3

8

In the case you want the last column to have as many elements as posible to fit the other columns (the most dynamic case for this problem), this would be a solution:

<?php

$numberOfColumns = 3;
$myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25];

$arrayLength = count($myArray); // 25
$columnLength = ceil($arrayLength / $numberOfColumns); // 9

for ($i = 0; $i < $columnLength; $i++) {

  for ($j = 0; $j < $numberOfColumns; $j++) {

    $arrayIndex = $j * $columnLength + $i;
    if ($arrayIndex < $arrayLength) {
      echo $myArray[$arrayIndex] . " ";
    }

  }

  echo '<br>';
}

This is the output in my browser:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@BillKervaski It's a very good practice to accept the right answer in order to support users motivation. Please consider doing that here :)
1

You will don't believe it.

All you need is just HTML and CSS - https://jsfiddle.net/wz9kp6x0/

<div class="xtable">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  ...
  <div>24</div>
  <div>25</div>
</div>

and

.xtable {
  column-count: 3;
}

See also docs: https://www.w3schools.com/css/css3_multiple_columns.asp

Enjoy! ;)

Comments

0

Thanks nanocv! Here's your solution changed up a bit to directly answer the question:

$numberOfColumns = 3;
$myArray = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25);

$arrayLength = count($myArray); // 25
$columnLength = ceil($arrayLength / $numberOfColumns); // 9

for($i=0; $i<$columnLength; $i++) {

    $table .= '<tr>';

    for ($j = 0; $j < $numberOfColumns; $j++) {

        $arrayIndex = $j * $columnLength + $i;

        if($arrayIndex < $arrayLength) {
            $table .= '<td>' . $myArray[$arrayIndex] . '</td>';
        } else {
            $table .= '<td></td>'; // render empty cells
        }

    }

    $table .= '</tr>';
}

echo '<table border="1">' . $table . '</table>'; 

1 Comment

Sorry, I didn't know you wanted to build a HTML table with the data. I'm happy you could find the solution to your problem! Please, consider accepting my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.