Developer Snippet Diary

convert given value to multiple integer parts valToDivisions(10,3)

valToDivisions() CONVERT given value to multiple parts

Code

/***
 * It will return values in equal parts but not in floting point it returns in integer
 * ie divide 10 into 3 parts will be 3,3,4
 ***/
valToDivisions(10,3);
function valToDivisions($value,$divisions){
    $quotient = floor($value / $divisions);
    $remainder = $value % $divisions;

    $result = array_fill(0, $divisions, $quotient);
    for ($i = 0; $i < $remainder; $i++) {
        $result[$i]++;
    }
    print_r($result);
}

Output

Array
(
    [0] => 4
    [1] => 3
    [2] => 3
)
Posted by: R GONDAL
Email: rizikmw@gmail.com