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

前端 未结 5 2102
醉话见心
醉话见心 2020-12-11 06:56

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

相关标签:
5条回答
  • 2020-12-11 07:33

    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.

    0 讨论(0)
  • 2020-12-11 07:45

    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];
        }
    }
    
    0 讨论(0)
  • 2020-12-11 07:49

    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  
    
    0 讨论(0)
  • 2020-12-11 07:53

    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

    0 讨论(0)
  • 2020-12-11 07:57
    $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
    
    0 讨论(0)
提交回复
热议问题