dtebrq0245 2011-10-19 08:04
浏览 15

如何将数组转换成表?

Take the following array:

$rows = array(
  'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5,
  'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10
);

I want to put this in a table, reading from top to bottom and left to right, like so:

a 1 c 3 e 5 g 7 i 9
b 2 d 4 f 6 h 8 j 10

As input for my table-creating function, I need the array to be transformed into one in which each item describes a table row, as follows:

Array(
[0] => Array(
    a => 1
    c => 3
    e => 5
    g => 7
    i => 9
)
[1] => Array(
    b => 2
    d => 4
    f => 6
    h => 8
    j => 10
)

The following code is my attempt, but I can't get it to work fully:

$cols_length = 5; 
$rows_length = (int) ceil(count($rows) / $cols_length); // 2
$keys = array_keys($rows);
$values = array_values($rows);

$output = array(); 
for ($i = 0; $i < $rows_length; $i++) { 
  for ($j = 0; $j < $cols_length; $j++) { 
    $key = $i + ($j * $rows_length); 
    if (array_key_exists($key, $keys)) { 
      $output[$i][$keys[$i + $j]] = $values[$key]; 
    } 
  } 
} 

print_r($output); 

It now outputs:

Array(
[0] => Array
    (
        [c] => 1
        [d] => 3
        [e] => 5
        [f] => 7
        [g] => 9
    )

[1] => Array
    (
        [d] => 2
        [e] => 4
        [f] => 6
        [g] => 8
        [h] => 10
    )
)

What am I doing wrong?

[edit]

Thank you for your responses. It works great :) I have made a mistake, however. The output array should be constructed as follows:

Array (
    [0] => Array (
        [0] => a
        [1] => 1
        [2] => c
        [3] => 3
        [4] => e
        [5] => 5
        [6] => g
        [7] => 7
        [8] => i
        [9] => 9
    )
    [1] => Array (
        [0] => b
        [1] => 2
        [2] => d
        [3] => 4
        [4] => f
        [5] => 6
        [6] => h
        [7] => 8
        [8] => j
        [9] => 10
    )
)

I have tried to modify the code to support this output, but I'm getting weird results. Should I use an additional $j++ counter?

$i = 0;
foreach($rows as $key => $value) {
  $result[$i++ % 2][] = $key;
  $result[$i++ % 2][] = $value;
}
  • 写回答

1条回答 默认 最新

  • dououde4065 2011-10-19 08:09
    关注

    You can use the modulus operator (%) to determine whether the element should be on first or second row:

    $rows = array(
        'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5,
        'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10
    );
    
    $result = array();
    
    $i = 0;
    foreach($rows as $key => $value) {
        if(!isset($result[$i % 2])) {
            $result[$i % 2] = array();
        }
        $result[$i % 2][$key] = $value;
        $i++;
    }
    
    print_r($result);
    

    Working example:

    http://codepad.org/Xz2CWYof

    This works even if you need more rows, just increase the % 2 to how many rows you need.

    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单