1

Hi I have a 1D array (1 by 20) that I would like to transform to a 2D Array (4 by 5)

$winning_number  = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

to

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

right now I am using this code:

foreach ($wining_no as $boulex)
{

    for($i=0;$i<$5;$i++)
    {
        if($i==0)
        {
            for($j=0;$j<$4;$j++)
            {   
                $boule_array[$j][$i] = $boulex;
            }
        }
    }
}

For some reason this does not work

3
  • 3
    array_chunk($winning_no, 5); Commented Oct 13, 2014 at 21:25
  • Maybe the problem is the typo: $4; inside of the third loop, in the condition. $j=0;$j<$4;$j++. Because I think you mean the integer 4. Commented Oct 13, 2014 at 21:28
  • Same as on line 4 $5 -> 5 Commented Oct 13, 2014 at 21:29

1 Answer 1

2

You could use the array_chunk($array, $size) function For you it would be like this

array_chunk($winning_number, 5);
Sign up to request clarification or add additional context in comments.

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.