Array into chunks and results by chunks in a new line with php

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Array Chunks: How to Format Data Line by Line in PHP

As developers, we frequently deal with raw data structures, and one common task is taking a flat array and restructuring it into meaningful, digestible chunks. The challenge often lies not just in splitting the array (which functions like array_chunk()), but in intelligently combining those resulting pieces to achieve a specific, formatted output—like concatenating elements within each chunk before printing them on separate lines.

This post will walk you through the exact method to achieve the desired result: taking an array and displaying its segments line by line, effectively grouping the data as you wish. We’ll move beyond the basic array_chunk() output to explore a more sophisticated solution using PHP string manipulation.

The Initial Approach: Understanding array_chunk()

You correctly started with the most straightforward tool for splitting arrays: array_chunk().

Given your input array:

$my_array = ["a", "b", "c", "d", "e", "f"];
$chunks = array_chunk($my_array, 3);
// Result: Array(0 => array(0 => "a"1 => "b"2 => "c")1 => array(4 => "d"5 => "e"6 => "f"))

This method successfully splits the data into groups of three. However, as you observed, this only gives you the raw chunks. To get the desired output (like abc followed by def), we need an extra step: combining the elements within each chunk into a single string before printing it.

The Solution: Combining Chunks with implode()

The key to solving this lies in using the implode() function. implode() is perfect for taking an array and joining all its elements into a single string, using a specified separator. By applying implode() to each chunk generated by array_chunk(), we achieve the desired contiguous output.

Here is the complete, robust solution:

<?php

$my_array = ["a", "b", "c", "d", "e", "f"];
$chunk_size = 3;
$formatted_output = [];

// 1. Chunk the array
$chunks = array_chunk($my_array, $chunk_size);

// 2. Process each chunk to join elements into a single string
foreach ($chunks as $chunk) {
    // Use implode() to join the elements of the current chunk with an empty string (no separator)
    $formatted_line = implode('', $chunk);
    $formatted_output[] = $formatted_line;
}

// 3. Print the final result, separated by newlines
echo implode("\n", $formatted_output);

?>

Step-by-Step Explanation

  1. Chunking: We first use array_chunk($my_array, 3) to separate our data into logical groups: [["a", "b", "c"], ["d", "e", "f"]].
  2. Imdoling: We then iterate through this $chunks array. Inside the loop, for each $chunk (e.g., ["a", "b", "c"]), we call implode('', $chunk). Since we use an empty string as the delimiter, it concatenates the elements directly: "abc".
  3. Final Assembly: We collect these resulting strings into a new array ($formatted_output) and finally use another implode("\n", $formatted_output) to print them, ensuring each result is on a new line.

Final Output Verification

When the code above is executed, the output will be:

abc
def

This method successfully transforms the segmented data into the seamless, line-by-line format you were aiming for. This concept of transforming intermediate results before final display is fundamental to writing clean, maintainable code, a principle strongly echoed in frameworks like Laravel, which emphasizes elegant solutions for complex tasks by promoting readable architecture. For more advanced array manipulation and collection handling within your Laravel applications, understanding these core PHP functions remains the bedrock.

Conclusion

The mystery of chunking arrays often boils down to knowing how to manipulate the resulting pieces. By combining array_chunk() with the powerful string joining capability of implode(), we can elegantly transform raw data into highly formatted output. This technique is not just a neat trick; it’s a core programming skill that allows you to handle complex data transformations efficiently and readable, which is essential whether you are working on backend logic or front-end presentation layers like those found in Laravel.