How does php split a two-dimensional array into several arrays?

Source: Internet
Author: User
How to split the following two-dimensional array into several arrays {code ...} $ arrnew [0] array ({code ...}); $ arrnew [1] array ({code ...}); $ arrnew [2] array ({code ...}); how to split the following two-dimensional array into several arrays

$ Arr = array ('id' => 1, 'User _ name' => 'test1'), array ('id' => 2, 'user _ name' => 'test2'), array ('id' => 3, 'User _ name' => 'test3 '),, array ('id' => 4, 'User _ name' => 'test2'), array ('id' => 5, 'user _ name' => 'test3'); after the split, the number is not certain, and two or three in each group are not necessarily

$ Arrnew [0] = array (

array('id' => 1,'user_name'=>'test1'),array('id' => 2,'user_name'=>'test2')

);
$ Arrnew [1] = array (

array('id' => 3,'user_name'=>'test3'),,array('id' => 4,'user_name'=>'test2')

);
$ Arrnew [2] = array (
Array (

'id' => 5,'user_name'=>'test3')

);

Reply content:

How to split the following two-dimensional array into several arrays

$ Arr = array ('id' => 1, 'User _ name' => 'test1'), array ('id' => 2, 'user _ name' => 'test2'), array ('id' => 3, 'User _ name' => 'test3 '),, array ('id' => 4, 'User _ name' => 'test2'), array ('id' => 5, 'user _ name' => 'test3'); after the split, the number is not certain, and two or three in each group are not necessarily

$ Arrnew [0] = array (

array('id' => 1,'user_name'=>'test1'),array('id' => 2,'user_name'=>'test2')

);
$ Arrnew [1] = array (

array('id' => 3,'user_name'=>'test3'),,array('id' => 4,'user_name'=>'test2')

);
$ Arrnew [2] = array (
Array (

'id' => 5,'user_name'=>'test3')

);

Array_chunk ($ arr, $ num); // $ num is 2 or 3

Check whether the following logic is correct.

$ Arr = array ('id' => 1, 'User _ name' => 'test1'), array ('id' => 2, 'user _ name' => 'test2'), array ('id' => 3, 'User _ name' => 'test3 '), array ('id' => 4, 'User _ name' => 'test2'), array ('id' => 5, 'user _ name' => 'test3'); rsort ($ arr); $ newArr = []; while (! Empty ($ arr) {$ count = rand (2, 3);/to control an array, set two or three elements. $ tmpArr = []; for ($ I = 0; $ I <$ count; $ I ++) {$ tmpArr [] = array_pop ($ arr) ;}$ newArr [] = $ tmpArr ;} dump ($ newArr );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.