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
...
...
...
...
...
...
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
Hi bro try my code if it fits to your needs. You just need to add more array data if you want to limit the use of each data.
<?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>';