如何在三列分布数组的值? [英] How do I distribute values of an array in three columns?

查看:144
本文介绍了如何在三列分布数组的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这个输出。

  1 3 5
2 4 6

我要使用像阵列功能阵列(1,2,3,4,5,6)。如果我修改这个数组像阵列(1,2,3),这意味着输出需要出示像

  1 2 3

这个概念只有3列是最大的。如果我们给阵列(1,2,3,4,5),这意味着输出应该是

  1 3 5
2 4

假设我们会给阵列(1,2,3,4,5,6,7,8,9),那么就意味着输出

  1 4 7
2 5 8
3 6 9

这是只有3列,最大。取决于给定的输入,该行将与3列来创建。

这是可能的PHP?我做的小研究与;发展阵列功能。我认为这是可能的。你能帮助我吗?

有关更多的信息:结果
*输入:阵列(1,2,3,4,5,6,7,8,9,10,11,12,13,14)结果
*输出:

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


解决方案

您可以做一个循环,它会自动将每三个要素一个新行:

  $值=阵列(1,1,1,1,1);的foreach($值$ I => $值){
  的printf(% - 4D',$值);  如果($ I%3 === 2)回声\\ n;
}

编辑:由于您添加了更多的信息,这里有你想要什么:

  $值=阵列(1,2,3,4,5);为($行= 0; $线2; $线++){
  如果($线== 0!)回声\\ n;  为($ I = $行; $ I<计数($值); $ I + = 2){
    的printf(% - 4D',$值[$ i]);
  }
}

如果你想都捆绑在一个函数:

 函数print_values​​_table($数组$线= 3,$ FORMAT =%-4D){
  $值= array_values​​($数组);
  $数= COUNT($值);  为($行= 0; $线474; $线; $行++){
    如果($线== 0!)回声\\ n;    为($ I = $行; $ I< $计数; $ I + = $行){
      的printf($格式,$值[$ i]);
    }
  }
}

编辑2:下面是修改后的版本,这将限制列数为3

 函数print_values​​_table($数组$ maxCols = 3,$ FORMAT =%-4D){
  $值= array_values​​($数组);
  $数= COUNT($值);
  $线= CEIL($计数/ $ maxCols);  为($行= 0; $线474; $线; $行++){
    如果($线== 0!)回声\\ n;    为($ I = $行; $ I< $计数; $ I + = $行){
      的printf($格式,$值[$ i]);
    }
  }
}

因此​​,以下内容:

  $值=范围(1,25);
print_array_table($值);

将输出这样的:

  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日

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  

解决方案

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  

这篇关于如何在三列分布数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆