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条)

报告相同问题?