Legionar - 1 year ago 77
PHP Question

# Divide N elements in 3 columns by columns

I need to write N elements (1 .. N) into 3 columns and need to get 3 values of elements, which are last in each column.

I need to get this:

`````` N ... array I need to get
--------------------
1 ... array(1)       // or array(1, 1, 1)
2 ... array(1, 2)    // or array(1, 2, 2)
3 ... array(1, 2, 3)
4 ... array(2, 3, 4)
5 ... array(2, 4, 5)
6 ... array(2, 4, 6)
7 ... array(3, 5, 7)
8 ... array(3, 6, 8)
9 ... array(3, 6, 9)
10 ... array(4, 7, 10)
11 ... array(4, 8, 11)
12 ... array(4, 8, 12)
13 ... array(5, 9, 13)
...etc
``````

f.e., if
`N = 10`
, I need to get result
`array(4, 7, 10)`
and the output table will be like this:

``````1    5    8
2    6    9
3    7    10
4
``````

PS:
not like this:

``````1    5    9
2    6    10
3    7
4    8
``````

I wrote this code, but it has problems with
`N = 10`
. Could someone help me or find there a problem in that big condition? Or just make it simplier? Many, many thanks.

``````\$count_all = 10; // this is N = 10

\$count = floor((\$count_all - 1) / 3) + 1;

\$i = 0;
\$j = 1;

echo '<div style="float: left;">';
for (\$k = 1; \$k <= 10; \$k++) {
echo '<div style="width: 230px;">';
echo \$k;
echo '</div>';

if (
(++\$i == \$count) or
((\$i == 1) and (\$count_all - \$i + 1 == \$count)) or
((\$i > 1) and (\$count_all - \$i == \$count)) or
(\$i + (\$j - 1) * \$count == \$count_all)
)  {
echo '</div>';
echo '<div style="float: left;">';

\$i = 0;
\$j++;
}
}
echo '</div>';
``````

Based on the output you want, you could do something like this (apply html accordingly):

``````<?php

\$n = 10;
\$cols = 3;

// Get number of cells in each column
\$num_left = \$n;
for (\$col = 0; \$col < \$cols; \$col++) {
\$num_rows[\$col] = ceil(\$num_left / (\$cols - \$col));
\$num_left -= \$num_rows[\$col];
}

\$cells = array();

for (\$i = 0; \$i < \$num_rows[0]; \$i++) {
for (\$j = 0; \$j < \$cols; \$j++) {
\$cells[\$i][\$j] = get_cell_number(\$num_rows, \$n, \$i, \$j);
}
}

print_r(\$cells);

function get_cell_number(Array \$num_rows, \$n, \$row, \$col) {
// Make sure cell isn't empty
if (\$row + 1 > \$num_rows[\$col]) {
return null;
}

// Get number in current cell
\$number = 0;
for (\$i = 0; \$i < \$col; \$i++) {
\$number += \$num_rows[\$i];
}
\$number += \$row + 1;

return \$number;
}
``````

This will produce an array of rows and columns like this:

``````Array
(
[0] => Array
(
[0] => 1
[1] => 5
[2] => 8
)

[1] => Array
(
[0] => 2
[1] => 6
[2] => 9
)

[2] => Array
(
[0] => 3
[1] => 7
[2] => 10
)

[3] => Array
(
[0] => 4
[1] =>
[2] =>
)

)
``````

If you only want the last value of each column, you could do something like this:

``````\$cells = array();

foreach (\$num_rows as \$col => \$num) {
\$cells[] = get_cell_number(\$num_rows, \$n, \$num - 1, \$col);
}

print_r(\$cells);
``````

Which outputs:

``````Array
(
[0] => 4
[1] => 7
[2] => 10
)
``````

You can also change the value of `\$n` and `\$cols` as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download