Pr0no
Pr0no

Reputation: 4109

How to transpone array into table?

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

Upvotes: 0

Views: 373

Answers (1)

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124888

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.

Upvotes: 2

Related Questions