dimanche 21 janvier 2018

Selecting multiple & random values with limit

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
...
...
...
...
...
...




Aucun commentaire:

Enregistrer un commentaire