array_chunk(): Breaking array into chunks

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 1

$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 

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 


plus2net.com




Post your comments , suggestion , error , requirements etc here .




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