Dividing a integer equally in X parts
math, php
Solution
Here's the algorithm you're looking for; it evenly spreads an integer `N` over `K` cells:
for i = 0 to K
array[i] = N / K # integer division
# divide up the remainder
for i = 0 to N mod K
array[i] += 1
Problem
I'm looking for a efficient way in PHP to divide a number in equal part. Number will always be integer (no float). Let's say that I have an array $hours with values from "1" to "24" ($hours['1'], etc) and a variable $int containing an integer. What I want to acheive is spreading the value of $int equally in 24 parts so I can assing the value to each corresponding array entries. (Should the number be odd, the remaining would be added to the last or first values in the 24). Regards,