PHP函数array_chunk()分割数组

array_chunk() 把一个数组分割成新的数组块

array array_chunk(array $array,int $size[,bool preserve_key])

参数描述:array是分割的数组

size规定每个新数组包含的元素,最后一个数组可能会少几个元素

preserve_key:可选,有两个选项:true,保留原始数组中的键名;false,默认选项,每个结果数组使用从0开始的新数组索引

示例:

$array = array(
    'a'=>'JAVA',
    'b'=>'PHP',
    'c'=>"C++",
    'd'=>'c#',
    'e'=>'perl'
);
print_r(array_chunk($array,2,false));
echo PHP_EOL;
print_r(array_chunk($array,2,true));

运行结果如下:

Array ( [0] => Array ( [0] => JAVA [1] => PHP ) [1] => Array ( [0] => C++ [1] => c# ) [2] => Array ( [0] => perl ) )

Array ( [0] => Array ( [a] => JAVA [b] => PHP ) [1] => Array ( [c] => C++ [d] => c# ) [2] => Array ( [e] => perl ) )
Responses / Cancel Reply
  1. Greetings from Colorado! I'm bored to death at work so I decided to browse your site on my iphone during lunch break. I really like the information you present here and can't wait to take a look when I get home. I'm shocked at how quick your blog loaded on my mobile .. I'm not even using WIFI, just 3G .. Anyhow, superb blog!

    Reply
  2. Hello there! I could have sworn I've been to this site before but after going through a few of the posts I realized it's new to me. Anyhow, I'm certainly happy I found it and I'll be bookmarking it and checking back regularly!

    Reply
  3. Yesterday, while I was at work, my sister stole my iPad and tested to see if it can survive a 40 foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is totally off topic but I had to share it with someone!

    Reply
  4. daftar slot

    Reply
  5. I pay a quick visit day-to-day some blogs and blogs to read posts, except this web site presents feature based content.

    Reply
  6. This is the perfect web site for everyone who wants to understand this topic. You realize a whole lot its almost tough to argue with you (not that I personally will need to…HaHa). You certainly put a new spin on a subject that has been written about for many years. Great stuff, just wonderful!

    Reply