简体   繁体   中英

PHP :1D array to 2D array

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

您可以使用array_chunk($array, $size)函数,您会像这样

array_chunk($winning_number, 5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM