Answer the question
In order to leave comments, you need to log in
Smart sorting of an array with the output of 5 elements without repetition. Is it so?
So I used this feature.
// Сортируем по весу
static function get_sort($names, $n, $ret = array()) {
if ($n == 0) {
return $ret;
} else {
$totalchance = 0;
$partialsums = [];
foreach ($names as $name) {
$totalchance += $name[1];
array_push($partialsums, $totalchance);
}
$chance = rand(0, $totalchance);
$nameindex = 0;
foreach ($partialsums as $ps) {
if ($ps >= $chance) {
break;
}
$nameindex++;
}
$name = $names[$nameindex];
array_push($ret, $name);
unset($names[$nameindex]);
$names = array_values($names);
return self::get_sort($names, $n-1, $ret);
}
}
$ids = class::get_sort($user_id_list, 5);
Array
(
[0] => Array
(
[0] => 273904544
[1] => 0.4
)
[1] => Array
(
[0] => 376650423
[1] => 0.78
)
[2] => Array
(
[0] => 396253653
[1] => 0
)
......
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question