Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I distribute values of an array in three columns?

Tags:

arrays

php

I need this output..

1 3 5
2 4 6

I want to use array function like array(1,2,3,4,5,6). If I edit this array like array(1,2,3), it means the output need to show like

1 2 3

The concept is maximum 3 column only. If we give array(1,2,3,4,5), it means the output should be

1 3 5 
2 4

Suppose we will give array(1,2,3,4,5,6,7,8,9), then it means output is

1 4 7
2 5 8
3 6 9

that is, maximum 3 column only. Depends upon the the given input, the rows will be created with 3 columns.

Is this possible with PHP? I am doing small Research & Development in array functions. I think this is possible. Will you help me?

For more info:
* 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  
like image 967
Ramkumar M Avatar asked Jun 23 '11 07:06

Ramkumar M


People also ask

Is it possible to create an array with different number of columns?

Yes you can. Array of double pointers.

What comes first in arrays rows or columns?

In the standard mathematical notation used for linear algebra, the first dimension of an array (or matrix) is the row, and the second dimension is the column.

What is a columns in an array?

An array is a way to represent multiplication and division using rows and columns. Rows represent the number of groups. Columns represent the number in each group or the size of each group.


5 Answers

You can do a loop that will automatically insert a new line on each three elements:

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

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

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

EDIT: Since you added more information, here's what you want:

$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]);
  }
}

And if you want to bundle all that in a function:

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

EDIT 2: Here is a modified version which will limit the numbers of columns to 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]);
    }
  }
}

So, the following:

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

Will output this:

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  
like image 98
Andrew Moore Avatar answered Oct 28 '22 10:10

Andrew Moore


One solution is to cut the array into chunks, representing the columns, and then print the values in row order:

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

If you don’t want to split your array, you can also do it directly:

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];
    }
}
like image 43
Gumbo Avatar answered Oct 28 '22 08:10

Gumbo


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

or

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

or

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

or, perhaps if you want a longer array split into columns of 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 ";
} 

which gives

1 2 3 
4 5 6 
7 8 9 
10 11 12 
13
like image 30
Dycey Avatar answered Oct 28 '22 09:10

Dycey


one solution is : your array has N elements, and you want 3 columns, so you can get the value of each cell with $myarray[ column_index + (N/3) + line_index ] (with one or two loops for columns and lines, at least for lines) I hope this will help you Bye

like image 27
Etienne Avatar answered Oct 28 '22 08:10

Etienne


Here's something I whipped up. I'm pretty sure this could be more easily accomplished if you were using HTML lists, I've assumed you can't use them.

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

NOTE : You can easily refactor the code inside the loop that uses $nexkey variable by making it into a loop itself so that it works for any number of columns. I've hardcoded it.

Uses loops now.

like image 42
JohnP Avatar answered Oct 28 '22 08:10

JohnP