选择有限制的多个随机值
Selecting multiple & random values with limit
我这里有这个代码:
$ids = implode(',', array_rand(array_column($test, 'id', 'id'), 5));
以上代码的作用如下:
"First extract the id column indexing also by the id, then pick 5 random ones, and finally implode into a comma separated list. Since keys must be unique, this has the added benefit of not returning duplicate ids if there happen to be duplicates in the array" 这是我的
现在,如果我将 $arr
从我的旧问题更改为:
Array
(
[id] => 13
[pets] => 8
[num_of_times] => 3
)
Array
(
[id] => 15
[pets] => 8
[num_of_times] => 6
)
Array
(
[id] => 16
[pets] => 10
[num_of_times] => 2
)
Array
(
[id] => 17
[pets] => 9
[num_of_times] => 4
)
Array
(
[id] => 18
[pets] => 10
[num_of_times] => 3
)
Array
(
[id] => 19
[pets] => 10
[num_of_times] => 10
)
Array
(
[id] => 20
[pets] => 0
[num_of_times] => 11
)
Array
(
[id] => 21
[pets] => 8
[num_of_times] => 9
)
Array
(
[id] => 22
[pets] => 9
[num_of_times] => 0
)
Array
(
[id] => 23
[pets] => 4
[num_of_times] => 3
)
Array
(
[id] => 24
[pets] => 0
[num_of_times] => 1
)
Array
(
[id] => 40
[pets] => 8
[num_of_times] => 0
)
Array
(
[id] => 43
[pets] => 2
[num_of_times] => 2
)
num_of_times
是可以选择id
或"user"的次数。
所以如果我有这样的 for loop
:
for ($i = 1; $i <= 10; $i++) {
$ids = implode(',', array_rand(array_column($arr, 'id', 'id'), 5));
echo $ids;
}
我怎样才能确保,例如,id 为 13 的第一个数组 NOT 进入 $ids
more超过 3 次,但可以进入 $ids
3 次或更少,当处于 for loop
时? (这适用于所有 ID)
例如,最终结果是这样的:
13,15,17,19,23
13,21,22,40,43
13,15,17,19,23
15,23,24,40,43 // 13 cannot be selected anymore because it already hit the "num_of_times" limit which is 3 for the number 13. Same thing for all the other numbers/id's
...
...
...
...
...
...
这是解决这个问题的一种方法
$num = array_column($test, 'num_of_times', 'id'); // get each num_of_times with id as index
for ($ids = '', $i = 1; $i <= 10; $i++) { // Your for loop
$num = array_filter($num); // Filter $num array
$rand = array_rand($num, 5); // get 5 random element
$ids .= implode(',', $rand) . '<br>'; // implode then concatenate to $ids var
foreach ($rand as $id) { // Run a foreach loop to $rand array
$num[$id]--; // Decrement num_of_times
}
}
echo $ids; // Echo result
嗨,兄弟,试试我的代码,看看它是否符合你的需要。如果你想限制每个数据的使用,你只需要添加更多的数组数据。
<?php
$arr = Array(Array
(
'id' => 13,
'pets' => 8,
'num_of_times' => 3
),Array(
'id' => 15,
'pets' => 8,
'num_of_times' => 6
),Array (
'id' => 16,
'pets' => 10,
'num_of_times' => 2
),Array(
'id' => 17,
'pets' => 9,
'num_of_times' => 4
),Array(
'id' => 18,
'pets' => 10,
'num_of_times' => 3
),Array(
'id' => 19,
'pets' => 10,
'num_of_times' => 10
),Array(
'id' => 20,
'pets' => 0,
'num_of_times' => 11
),Array(
'id' => 21,
'pets' => 8,
'num_of_times' => 9
),Array(
'id' => 22,
'pets' => 9,
'num_of_times' => 0
),Array(
'id' => 23,
'pets' => 4,
'num_of_times' => 3
),Array(
'id' => 24,
'pets' => 0,
'num_of_times' => 1
),Array(
'id' => 40,
'pets' => 8,
'num_of_times' => 0
),Array(
'id' => 43,
'pets' => 2,
'num_of_times' => 2,
));
$checker = array(); //array that holds the id with the count of use
$remove =array(); // array to remove in array to random
for ($i = 1; $i <= 10; $i++) {
$newArray =array_diff(array_column($arr, 'id', 'id'),$remove); //the newarray to use in random with remove function for the id that has been used for 3x
$ids = array_rand($newArray, 5);
foreach($ids as $id){
if(!array_key_exists($id,$checker)){ // check if the id is existing in the checker key if yes
$checker[$id] = 1; // insert value 1 in the array as the count of use
}else{
if(!empty($checker[$id]) && $checker[$id] < 3){ //check if the array has a value lower than 3
$checker[$id]=$checker[$id]+1; // then add 1 value for the data as a increment of data usage
if($checker[$id] == 3){ //check if da is already used 3x
$remove[]=$id; //then add it to the variable remove
}
}
}
}
echo implode(',',$ids).'</br>';
}
echo '<pre>';
echo print_r($checker);
echo '</pre>';
我这里有这个代码:
$ids = implode(',', array_rand(array_column($test, 'id', 'id'), 5));
以上代码的作用如下:
"First extract the id column indexing also by the id, then pick 5 random ones, and finally implode into a comma separated list. Since keys must be unique, this has the added benefit of not returning duplicate ids if there happen to be duplicates in the array" 这是我的
现在,如果我将 $arr
从我的旧问题更改为:
Array
(
[id] => 13
[pets] => 8
[num_of_times] => 3
)
Array
(
[id] => 15
[pets] => 8
[num_of_times] => 6
)
Array
(
[id] => 16
[pets] => 10
[num_of_times] => 2
)
Array
(
[id] => 17
[pets] => 9
[num_of_times] => 4
)
Array
(
[id] => 18
[pets] => 10
[num_of_times] => 3
)
Array
(
[id] => 19
[pets] => 10
[num_of_times] => 10
)
Array
(
[id] => 20
[pets] => 0
[num_of_times] => 11
)
Array
(
[id] => 21
[pets] => 8
[num_of_times] => 9
)
Array
(
[id] => 22
[pets] => 9
[num_of_times] => 0
)
Array
(
[id] => 23
[pets] => 4
[num_of_times] => 3
)
Array
(
[id] => 24
[pets] => 0
[num_of_times] => 1
)
Array
(
[id] => 40
[pets] => 8
[num_of_times] => 0
)
Array
(
[id] => 43
[pets] => 2
[num_of_times] => 2
)
num_of_times
是可以选择id
或"user"的次数。
所以如果我有这样的 for loop
:
for ($i = 1; $i <= 10; $i++) {
$ids = implode(',', array_rand(array_column($arr, 'id', 'id'), 5));
echo $ids;
}
我怎样才能确保,例如,id 为 13 的第一个数组 NOT 进入 $ids
more超过 3 次,但可以进入 $ids
3 次或更少,当处于 for loop
时? (这适用于所有 ID)
例如,最终结果是这样的:
13,15,17,19,23
13,21,22,40,43
13,15,17,19,23
15,23,24,40,43 // 13 cannot be selected anymore because it already hit the "num_of_times" limit which is 3 for the number 13. Same thing for all the other numbers/id's
...
...
...
...
...
...
这是解决这个问题的一种方法
$num = array_column($test, 'num_of_times', 'id'); // get each num_of_times with id as index
for ($ids = '', $i = 1; $i <= 10; $i++) { // Your for loop
$num = array_filter($num); // Filter $num array
$rand = array_rand($num, 5); // get 5 random element
$ids .= implode(',', $rand) . '<br>'; // implode then concatenate to $ids var
foreach ($rand as $id) { // Run a foreach loop to $rand array
$num[$id]--; // Decrement num_of_times
}
}
echo $ids; // Echo result
嗨,兄弟,试试我的代码,看看它是否符合你的需要。如果你想限制每个数据的使用,你只需要添加更多的数组数据。
<?php
$arr = Array(Array
(
'id' => 13,
'pets' => 8,
'num_of_times' => 3
),Array(
'id' => 15,
'pets' => 8,
'num_of_times' => 6
),Array (
'id' => 16,
'pets' => 10,
'num_of_times' => 2
),Array(
'id' => 17,
'pets' => 9,
'num_of_times' => 4
),Array(
'id' => 18,
'pets' => 10,
'num_of_times' => 3
),Array(
'id' => 19,
'pets' => 10,
'num_of_times' => 10
),Array(
'id' => 20,
'pets' => 0,
'num_of_times' => 11
),Array(
'id' => 21,
'pets' => 8,
'num_of_times' => 9
),Array(
'id' => 22,
'pets' => 9,
'num_of_times' => 0
),Array(
'id' => 23,
'pets' => 4,
'num_of_times' => 3
),Array(
'id' => 24,
'pets' => 0,
'num_of_times' => 1
),Array(
'id' => 40,
'pets' => 8,
'num_of_times' => 0
),Array(
'id' => 43,
'pets' => 2,
'num_of_times' => 2,
));
$checker = array(); //array that holds the id with the count of use
$remove =array(); // array to remove in array to random
for ($i = 1; $i <= 10; $i++) {
$newArray =array_diff(array_column($arr, 'id', 'id'),$remove); //the newarray to use in random with remove function for the id that has been used for 3x
$ids = array_rand($newArray, 5);
foreach($ids as $id){
if(!array_key_exists($id,$checker)){ // check if the id is existing in the checker key if yes
$checker[$id] = 1; // insert value 1 in the array as the count of use
}else{
if(!empty($checker[$id]) && $checker[$id] < 3){ //check if the array has a value lower than 3
$checker[$id]=$checker[$id]+1; // then add 1 value for the data as a increment of data usage
if($checker[$id] == 3){ //check if da is already used 3x
$remove[]=$id; //then add it to the variable remove
}
}
}
}
echo implode(',',$ids).'</br>';
}
echo '<pre>';
echo print_r($checker);
echo '</pre>';