php - How many arrays does array_chunk return? -
how know amount of arrays php chunk function returns?
for example, if execute piece of code know it'll return 3 arrays, how calculate it:
$input_array = array('a', 'b', 'c', 'd', 'e'); print_r(array_chunk($input_array, 2));
output:
array ( [0] => array ( [0] => [1] => b ) [1] => array ( [0] => c [1] => d ) [2] => array ( [0] => e ) )
you can calculate programmatically dividing number of values in array should chunked chunk size:
$input_array = array('a', 'b', 'c', 'd', 'e'); $chunksize = 2; print_r(array_chunk($input_array, $chunksize)); $count = round(count($input_array) / $chunksize,php_round_up);
i'm using php_round_up
because last chunk doesn't neccessarly have chunk size values, array, too.
Comments
Post a Comment