Array ( [0] => apple [1] => banana [2] => cherry ) [1] => Array ( [0] => date [1] => elderberry [2] => fig ) [2] => Array ( [0] => grape [1] => honeydew [2] => kiwi ) [3] => Array ( [0] => lemon ) ) */ // Splitting with preserve_keys = true (useful for associative arrays) $associativeItems = [ 'a' => 'Apple', 'b' => 'Banana', 'c' => 'Cherry', 'd' => 'Date', 'e' => 'Elderberry' ]; $chunkSizeAssociative = 2; $chunksPreserveKeys = array_chunk($associativeItems, $chunkSizeAssociative, true); echo " Associative array split into chunks of $chunkSizeAssociative (preserving keys): "; print_r($chunksPreserveKeys); /* Output: Array ( [0] => Array ( [a] => Apple [b] => Banana ) [1] => Array ( [c] => Cherry [d] => Date ) [2] => Array ( [e] => Elderberry ) ) */ // Common use case: Paginate database results (assuming $allRecords is an array of records) $allRecords = [ ['id' => 1, 'name' => 'Record A'], ['id' => 2, 'name' => 'Record B'], ['id' => 3, 'name' => 'Record C'], ['id' => 4, 'name' => 'Record D'], ['id' => 5, 'name' => 'Record E'], ]; $recordsPerPage = 2; $currentPage = 2; // e.g., from $_GET['page'] $paginatedChunks = array_chunk($allRecords, $recordsPerPage); $recordsForCurrentPage = $paginatedChunks[$currentPage - 1] ?? []; // Subtract 1 for 0-based index echo " Records for page $currentPage: "; print_r($recordsForCurrentPage); /* Output (for currentPage = 2): Array ( [0] => Array ( [id] => 3 [name] => Record C ) [1] => Array ( [id] => 4 [name] => Record D ) ) */ ?>