I want to get random numbers with exceptions (for example, I want a number from 500 to 550 without 505, 512, 525, etc).
I found here one code like this:
function randWithout($from, $to, array $exceptions) {
sort($exceptions); // lets us use break; in the foreach reliably
$number = rand($from, $to - count($exceptions)); // or mt_rand()
foreach ($exceptions as $exception) {
if ($number >= $exception) {
$number++; // make up for the gap
} else {
break;
}
}
return $number;
}
If I use something like:
$nr = randWithout(500, 550, array(505, 512, 525));
everything it’s fine, but in array I want to put elements from mysql, so I have created:
$query = mysql_query("SELECT `site_id` FROM `table` WHERE `user_id`='{$data->id}'");
$data = array();
while ($issues_row = mysql_fetch_array($query, MYSQL_ASSOC)) {
$data[] = $issues_row['site_id'];
}
$result = implode(",", $data);
Now, if i use:
$nr = randWithout(500, 550, array($result));
it’s not working, so my problem is array($result)
What is wrong here?
Tim is right, do not implode results in a string.
But that is the quite odd function for getting random number with exceptions. Try something like: