1

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) 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) Means the output should be

1 3 5 
2 4

Suppose we will give array(1,2,3,4,5,6,7,8,9) Means o/p is

1 4 7
2 5 8
3 6 9

Maximum 3 Column only.Depends upon the the given input the rows will be create with 3 column.Is This is Possible with PHP?Am Doing small Research & Development in Array Functions.I think this possible.Will you help me? For more info:I/p :array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) O/P:

1  6   11 
2  7   12 
3  8   13 
4  9   14 
5  10  15
4
  • 1
    This is not a mathematics question. It looks like it would be more appropriate for stackoverflow, or one of the other stackexchange sites dealing with programming. Commented Jun 24, 2011 at 7:19
  • Will you please Migrate this question to Stackoverflow?I don't have permission to do that..Please..:) Commented Jun 24, 2011 at 7:20
  • 1
    I don't have the power to do that either, yet, but I have flagged for moderator attention, they will be able to. Commented Jun 24, 2011 at 7:22
  • Duplicate of stackoverflow.com/q/6450810/469210 Commented Jun 24, 2011 at 12:48

2 Answers 2

1

Getting number of rows is as easy as ceiling(total number of elements / elements per row). All you need to do is to copy the elements from one one-dimensional array to an other two-dimensional array. One possible method:

<?php
$input = array(
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
);
$col = 3;
$row = ceil(count($input) / $col);
$output = array();
while(count($input)){
    $output[] = array_splice($input, 0, $row);
}
for($i = 0; $i < $row; $i++) {
    $temp = array();
    for($j = 0; $j < $col; $j++) {
        if (isset($output[$j][$i])) {
            $temp[] = $output[$j][$i];
        }
    }
    echo implode(", ", $temp) . "\n";
}

Output

1, 6, 11
2, 7, 12
3, 8, 13
4, 9
5, 10
Sign up to request clarification or add additional context in comments.

Comments

1

use array_chunk() function: http://www.php.net/manual/en/function.array-chunk.php

$example_array = array('a', 'b', 'c', 'd', 'e');
$chunked_array = array_chunk($example_array, 3));

$chunked_array will be an array with 2 subarrays: array(array(a,b,c), array(d,e))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.