Skip to content

Instantly share code, notes, and snippets.

@permatis
Created December 11, 2015 19:40
Show Gist options
  • Save permatis/7e33804e3bc68fb173e2 to your computer and use it in GitHub Desktop.
Save permatis/7e33804e3bc68fb173e2 to your computer and use it in GitHub Desktop.
This function create for part or split amount a number or record from database.
<?php
/**
* Output like this :
* array('1' => array('1' => '1','2' => '2'), '2' => array('3' => '3', '4' => '4'), '3' => array('5' => '5'));
*/
function pecah_total($total, $limit)
{
for ($i=1; $i <= floor($total/$limit)+1; $i++) {
if($i == 1){
$arr = ''; foreach (range(1, $limit) as $val) { $arr[$val] = $val; } $angka[$i] = $arr;
}else{
if(($total - ($i*$limit-$limit)) > $limit){
$arr = ''; foreach (range($i*$limit-$limit+1, $i*$limit) as $val) { $arr[$val] = $val; } $angka[$i] = $arr;
}else{
$arr = ''; foreach (range($i*$limit-$limit+1, $total) as $val) { $arr[$val] = $val; } $angka[$i] = $arr;
}
}
}
return $angka;
}
var_dump(pecah_total(5, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment