How to distribute array values ​​in three columns?

I need this conclusion.

1 3 5
2 4 6

I want to use an array function, for example array(1,2,3,4,5,6). If I edit this array as array(1,2,3), it means that the output should be displayed as

1 2 3

A concept is a maximum of 3 columns. If we give array(1,2,3,4,5), it means that the conclusion must be

1 3 5 
2 4

Suppose we give array(1,2,3,4,5,6,7,8,9), then this means that the conclusion

1 4 7
2 5 8
3 6 9

i.e. a maximum of 3 columns. Depends on this input, rows will be created with three columns.

Is this possible with PHP? I do some research and development on array functions. I think it's possible. Will you help me?

For more information:
* input: array(1,2,3,4,5,6,7,8,9,10,11,12,13,14)
* output:

1  6   11 
2  7   12 
3  8   13 
4  9   14 
5  10  
+5
5

, :

$values = array(1,1,1,1,1);

foreach($values as $i => $value) {
  printf('%-4d', $value);

  if($i % 3 === 2) echo "\n";
}

EDIT:. , :

$values = array(1,2,3,4,5);

for($line = 0; $line < 2; $line++) {
  if($line !== 0) echo "\n";

  for($i = $line; $i < count($values); $i+=2) {
    printf('%-4d', $values[$i]);
  }
}

:

function print_values_table($array, $lines = 3, $format = "%-4d") {
  $values = array_values($array);
  $count = count($values);

  for($line = 0; $line < $lines; $line++) {
    if($line !== 0) echo "\n";

    for($i = $line; $i < $count; $i += $lines) {
      printf($format, $values[$i]);
    }
  }
}

2: , 3.

function print_values_table($array, $maxCols = 3, $format = "%-4d") {
  $values = array_values($array);
  $count = count($values);
  $lines = ceil($count / $maxCols);

  for($line = 0; $line < $lines; $line++) {
    if($line !== 0) echo "\n";

    for($i = $line; $i < $count; $i += $lines) {
      printf($format, $values[$i]);
    }
  }
}

, :

$values = range(1,25);
print_array_table($values);

:

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

, , :

$cols = array_chunk($arr, ceil(count($arr)/3));
for ($i=0, $n=count($cols[0]); $i<$n; $i++) {
    echo $cols[0][$i];
    if (isset($cols[1][$i])) echo $cols[1][$i];
    if (isset($cols[2][$i])) echo $cols[2][$i];
}

, :

for ($c=0, $n=count($arr), $m=ceil($n/3); $c<$m; $c++) {
    echo $arr[$c];
    for ($r=$m; $r<$n; $r+=$m) {
        echo $arr[$c+$r];
    }
}
+5
$a = array(1,2,3,4,5);
"{$a[0]} {$a[1]} {$a[2]}\n{$a[3]} {$a[4]}";

$a = array(1,2,3,4,5);
"{$a[0]} {$a[1]} {$a[2]}".PHP_EOL."{$a[3]} {$a[4]}";

$a = array(1,2,3,4,5);
$second_row_start = 3; // change to vary length of rows
foreach( $a as $index => $value) {
  if($index == $second_row_start) echo PHP_EOL;
  echo "$value ";
}

, , , 3?

$a = array(1,2,3,4,5,6,7,8,9,10,11,12,13);
$row_length = 3; // change to vary length of rows
foreach( $a as $index => $value) {
  if($index%$row_length == 0) echo PHP_EOL;
  echo "$value ";
} 

1 2 3 
4 5 6 
7 8 9 
10 11 12 
13
+3

: N , 3 , $myarray [column_index + (N/3) + line_index] ( , ) , Bye

+1

- . , , HTML, , .

$arr    = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14, 15, 16);
$max    = count($arr);
$cols   = 3;
$block  = ceil($max / $cols);
for ($i = 0; $i < $block ; $i++) {
    echo $arr[$i] . ' ';

    for ($j = 1; $j < $cols; $j++) {
        $nexKey = $i  + $block * $j;
        if (!isset($arr[$nexKey])) break;   
        echo $arr[$nexKey] . ' ';
    }
    echo '<br>';
}

. , $nexkey, , . .a >

.

+1

All Articles