Jump to content

Recommended Posts

Hi all. New here, and new to php. I have been working on a project and had some good success. I am using the array_chunk_vertical function and it works great. Now I need it to do a little bit more and I can't figure it out.

 

I have attached my working file for review. But basically, this is what I am working on. Here is my code:

<?php

function array_chunk_vertical($input, $size, $preserve_keys = false, $size_is_horizontal = true)
{
    $chunks = array();

    if ($size_is_horizontal) {
        $chunk_count = ceil(count($input) / $size);
    } else {
        $chunk_count = $size;
    }

    for ($chunk_index = 0; $chunk_index < $chunk_count; $chunk_index++) {
        $chunks[] = array();
    }

    $chunk_index = 0;
    foreach ($input as $key => $value)
    {
        if ($preserve_keys) {
            $chunks[$chunk_index][$key] = $value;
        } else {
            $chunks[$chunk_index][] = $value;
        }

        if (++$chunk_index == $chunk_count) {
            $chunk_index = 0;
        }
    }

    return $chunks;
}

$values = array(1,2,3,4,5,6,7,8,9,10,11,12);
$rows = array_chunk_vertical($values, 6);

print "<table>\n";
foreach ($rows as $row) {
    print "<tr>\n";
    foreach ($row as $value) {
        print "<td>" . $value . "</td>\n";
    }
    print "</tr>\n";
}
print "</table>\n";

?>

What I have will produce this result (using 6 columns in the function):

1   3   5  7   9   11

2   4   6  8  10  12

 

What I would like to be able to do is add another dimension and group them but the second dimension, like an employee number. So say I have the following data:

employee#   value1   value2   value3   value4   value5   value6   value7   value8   value9   value10   value11   value12

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

202                     2          4           6            8          10          12           14          16        18              20        22             24

 

I want the result to be (still using 6 columns):

101  5      10     4      14     24

1      6      11     6      16    

2      7      12     8      18

3      8    202    10     20

4      9        2    12     22

 

Any help for this newbie would be greatly appreciated!

columns.php

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.