Division number and remainder php code I created a function that will tell me how many times a number is divisible by another number and what the remainder of it is:

 &lt;?php
function divisible_times($number, $divider) {
    $count = 0;
    $remainder = $number % $divider;
    $num_array = range($number, 1);
    foreach($num_array as $num) {
        if($num % $divider == 0){
            $count++;
        }
    }

    $arr[$number.':'.$divider]['divisions_no'] = $count;
    $arr[$number.':'.$divider]['remainder'] = $remainder;
    return $arr;
}
print_r(divisible_times(4, 3)); // Returns Array ( [4:3] =&gt; Array ( [divisions_no] =&gt; 1 [remainder] =&gt; 1 ) )
print_r(divisible_times(14, 3)); // Returns Array ( [14:3] =&gt; Array ( [divisions_no] =&gt; 4 [remainder] =&gt; 2 ) )
print_r(divisible_times(13, 3)); // Returns Array ( [13:3] =&gt; Array ( [divisions_no] =&gt; 4 [remainder] =&gt; 1 ) )
print_r(divisible_times(12, 3)); // Returns Array ( [12:3] =&gt; Array ( [divisions_no] =&gt; 4 [remainder] =&gt; 0 ) )


Can this code be optimized somehow? I tried to use $number directly, but couldn't find out the count that way. The only thing I could think is to create am array of numbers and go through each one.

I mean this does the trick, but optimization is important, and I was wondering if there is any other way of doing it?
Try the following:

function divisible_times($number, $divider) {
  $arr[$number.':'.$divider]['divisions_no'] = floor($number / $divider);
  $arr[$number.':'.$divider]['remainder'] = $number % $divider;
  return $arr;
}