简体   繁体   中英

PHP - single dimension array to a multidimensional

I'm using jquery to send a serialized single dimensional array to my php script.

Upon arriving at the server: The array should be transformed into a multidimensional array where every 2 values in the original array are a new row.

   $oldarray

looks like (value1, value2, value3, value4)

  $newarray =array();

Should look like

(   Value1, Value2
    Value3, Value4  )

Any suggestions?

You can use array_chunk for this:

 $newarray = array_chunk($oldarray, 2);

The first example on the linked doc page shows this exact use case.

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