Reputation: 3549
I've got an array of 'contestant entrants' which can be shown like this:
$all_entrants = array(
array('username'=>'122', 'number_of_entries'=>1),
array('username'=>'123', 'number_of_entries'=>4),
array('username'=>'124', 'number_of_entries'=>3),
...
)
From these entries I need to create another array called $draw
.
The $draw
array will have username
repeated as many times as it's corresponding number_of_entries
. So for the above example it might look like this:
$draw = array("122", "123", "123", "123", "123", "124", "124", "124")
I want this so that I can later generate a random number, and find the winner by doing something like $draw[$randomNumber];
However I cannot get my head around how to create that $draw
array from the $all_entrants
array... Any help will be greatly appreciated!
Upvotes: 1
Views: 89
Reputation: 212522
$all_entrants = array(
array('username'=>'122', 'number_of_entries'=>1),
array('username'=>'123', 'number_of_entries'=>4),
array('username'=>'124', 'number_of_entries'=>3),
);
$draw = array();
foreach($all_entrants as $entrant) {
$draw = array_merge(
$draw,
array_fill(0, $entrant['number_of_entries'], $entrant['username'])
);
}
var_dump($draw);
Upvotes: 1
Reputation: 43
I think it's a question about select one from a group of name which has different weight. maybe an array like this
$group = array(
array('122' => 1),
array('123'=> 4),
array('124'=> 3)
);
First Calculate the sum of the weight, or may be it has been known already
$total_weight = 0;
foreach($group as $weight){
$total_weight += $weight;
}
Then generate a random number from 0 to $total_weight, eg. 0<=$rand_number
$current_total = 0;
foreach($group as $name => $weight){
if($rand_number <= $current_total)
return $name;
$current_total += $weight;
}
--
BTW, I'm new here, more to learn:)
Upvotes: 2
Reputation: 4142
check this :--
$result=array();
$all_entrants = array(
array('username'=>'122', 'number_of_entries'=>1),
array('username'=>'123', 'number_of_entries'=>4),
array('username'=>'124', 'number_of_entries'=>3)
);
foreach($all_entrants as $value)
for($i=0;$i<$value['number_of_entries'];$i++)
array_push($result,$value['username']);
echo '<pre>';
print_r($result);
output :-
Array
(
[0] => 122
[1] => 123
[2] => 123
[3] => 123
[4] => 123
[5] => 124
[6] => 124
[7] => 124
)
Upvotes: 0
Reputation: 3644
I assume you're looking for something like this?
$draw = array();
foreach($all_entrants as $entrant) // loop through array with entrants
for ($i = 0; $i<$entrant['number_of_entries']; $i++) //get number of entries
$draw[] = $entrant['username']; //add them to the $draw array
Upvotes: 5
Reputation: 369
<?php
$all_entrants = array(
array('username'=>'122', 'number_of_entries'=>1),
array('username'=>'123', 'number_of_entries'=>4),
array('username'=>'124', 'number_of_entries'=>3)
);
$draw = array();
for ($i = 0; $i < count($all_entrants); $i++)
{
$entrants = $all_entrants[$i];
$name = $entrants["username"];
$entry_count = $entrants["number_of_entries"];
for ($j = 0; $j < $entry_count; $j++) $draw[] = $name;
}
print_r($draw);
?>
Hope it helps.
Upvotes: 0
Reputation: 5216
Try this
$newarray=array();
foreach($all_entrants as $list){
for($i=1;$i<=$list['number_of_entries'];$i++){
array_push($newarray,$list['username']);
}
}
Upvotes: 0
Reputation: 6000
$draw = array();
foreach($all_entrants as $entrant) {
for($i=0; $i<$entrant['number_of_entries']; $i++) {
$draw[] = $entrant['username'];
}
}
Upvotes: 1
Reputation: 929
<?php
$draw = array();
foreach($all_entrants as $entrant) {
for($i=0; $i<$entrant['number_of_entries']; $i++) {
$draw[] = $entrant['username'];
}
}
Upvotes: 1