1

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!

8 Answers 8

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

@BenFortune you are correct, thank you, will fix it right away!
Thank you! Tried it and it's exactly what I need! Will mark as correct in a few mins.
2

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

Comments

1
<?php
$draw = array();
foreach($all_entrants as $entrant) {
    for($i=0; $i<$entrant['number_of_entries']; $i++) {
        $draw[] = $entrant['username'];
    }
}

Comments

1
$draw = array();
foreach($all_entrants as $entrant) {
    for($i=0; $i<$entrant['number_of_entries']; $i++) {
        $draw[] = $entrant['username'];
    }
}

Comments

1
$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);

1 Comment

best answer among all these answer.
0

Try this

$newarray=array();
foreach($all_entrants as $list){

for($i=1;$i<=$list['number_of_entries'];$i++){

  array_push($newarray,$list['username']);

   }



       }

Comments

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

Comments

0

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
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.