array_chunk(): Breaking array into chunks

$input=array('One','Two','Three','Four','Five');
$output=array_chunk($input,2);
echo print_r($output); // 

while (list ($key, $val) = each ($output)) {
echo "<br><b>$key -> $val</b> <br>";
while (list ($key1, $val1) = each ($val)) {
echo "$key1 -> $val1 <br>";
}
}
Output is here
Array ( [0] => Array ( [0] => One [1] => Two ) [1] => Array ( [0] => Three [1] => Four ) [2] => Array ( [0] => Five ) ) 1
0 -> Array 
0 -> One 
1 -> Two 

1 -> Array 
0 -> Three 
1 -> Four 

2 -> Array 
0 -> Five 
Syntax
array_chunk ($input_array , int $size [, bool $preserve_keys = FALSE ] )
ParameterDESCRIPTION
$input_arrayRequired : Input array to break
$sizeRequired : INT , Number of elements in output array or size of broken arrays.
$preserve_keysOptional :Boolean , default value is FALSE to reindex the output chunk numerically.
TRUE : The keys of $input_array are preserved.
Return value is multidimensional arrays, each dimension contains the $size number of elements. The last chunk may contain less than the $size of elements.

$size must be equal to or greater than 1.

Example 2 with $preserve_keys = TRUE

$input=array('One','Two','Three','Four','Five');
$output=array_chunk($input,2,true);
echo print_r($output); // 

while (list ($key, $val) = each ($output)) {
echo "<br><b>$key -> $val</b> <br>";
while (list ($key1, $val1) = each ($val)) {
echo "$key1 -> $val1 <br>";
}
}
Output is here
Array ( [0] => Array ( [0] => One [1] => Two ) [1] => Array ( [2] => Three [3] => Four ) [2] => Array ( [4] => Five ) ) 1
0 -> Array 
0 -> One 
1 -> Two 

1 -> Array 
2 -> Three 
3 -> Four 

2 -> Array 
4 -> Five 
Joining Two Arrays by array_merge
Array REFERENCE
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer