doushanmo7024 2018-01-22 02:25
浏览 58
已采纳

选择带限制的多个和随机值

I have this code here:

$ids = implode(',', array_rand(array_column($test, 'id', 'id'), 5));

Here's what that code above does:

"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" this is from my previous question

Now if I change my $arr from my old question to this:

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 is the number of times that id or "user" can be selected.

So if I had a for loop like this:

for ($i = 1; $i <= 10; $i++) {
    $ids = implode(',', array_rand(array_column($arr, 'id', 'id'), 5));
    echo $ids;
}

how can I make sure, for example, the first array with id 13 does NOT go into $ids more than 3 times but CAN go into $ids 3 times OR less, when in the for loop? (this applies for all the id's)

For example, the final result would be something like this:

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
...
...
...
...
...
...
  • 写回答

2条回答 默认 最新

  • douqujin2767 2018-01-22 04:48
    关注

    This is one way to solve this

    $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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题