PHP array_chunk()ฟังก์ชั่น
ตัวอย่าง
แบ่งอาร์เรย์ออกเป็นสองส่วน:
<?php
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
?>
ความหมายและการใช้งาน
ฟังก์ชัน array_chunk() แบ่งอาร์เรย์ออกเป็นชิ้น ๆ ของอาร์เรย์ใหม่
ไวยากรณ์
array_chunk(array, size, preserve_key)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
array | Required. Specifies the array to use |
size | Required. An integer that specifies the size of each chunk |
preserve_key | Optional. Possible values:
|
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับอาร์เรย์ที่จัดทำดัชนีหลายมิติ เริ่มต้นด้วยศูนย์ โดยแต่ละมิติจะมีองค์ประกอบขนาด |
---|---|
รุ่น PHP: | 4.2+ |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
แบ่งอาเรย์ออกเป็นสองส่วนและคงคีย์ดั้งเดิมไว้:
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
print_r(array_chunk($age,2,true));
?>
❮ การอ้างอิงอาร์เรย์ PHP