我們在學(xué)習(xí)字符串的時(shí)候,經(jīng)常會(huì)進(jìn)行切割元素的操作,當(dāng)然在數(shù)組中也是同樣可以進(jìn)行分割的。本篇所要帶來的是array_chunk() 函數(shù),可以說它就是專門用來分割數(shù)組的函數(shù)。下面我們對array_chunk()的概念、語法、參數(shù)、返回值進(jìn)行介紹,然后分享一個(gè)實(shí)例供大家學(xué)習(xí)。
1、概念
將一個(gè)數(shù)組分隔成若干個(gè)數(shù)組。
2、語法
codelayui.code
- array_chunk(array,size,preserve_keys);
3、參數(shù)
codelayui.code
- array
- size
- preserve_key
4、返回值
返回一個(gè)多維的數(shù)值數(shù)組,從 0 開始,每個(gè)維度都包含 size 元素。
5、實(shí)例
codelayui.code
- $array = ['name' => 'tom', 'age' => 20, 3, 4, 5, 'a', 'b'];
每3個(gè)分割一組
codelayui.code
- $chunk_result = array_chunk($array, 3);
結(jié)果
codelayui.code
- Array
- (
- [0] => Array
- (
- [0] => tom
- [1] => 20
- [2] => 3
- )
- [1] => Array
- (
- [0] => 4
- [1] => 5
- [2] => a
- )
- [2] => Array
- (
- [0] => b
- )
- )
以上就是PHP中array_chunk() 函數(shù)分割數(shù)組的方法,大家對基本的內(nèi)容部分學(xué)習(xí)完后,不妨展開代碼部分的練習(xí)吧。