$number) $last_spoken[(int)$number] = (int)$turn + 1; $last = array_keys($last_spoken)[count($last_spoken) - 1]; for ($i = count($last_spoken); $i < $num; $i++) { if (array_key_exists($last, $last_spoken)) { $prev = $last; $last = $i - $last_spoken[$last]; $last_spoken[$prev] = $i; } else { $last_spoken[$last] = $i; $last = 0; } } return $last; }