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!

4

8 回答 8

5

我假设你正在寻找这样的东西?

$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
于 2013-09-12T09:47:39.440 回答
2

我认为这是一个关于从一组具有不同权重的名称中选择一个的问题。也许像这样的数组

$group = array(
    array('122' => 1),
    array('123'=> 4),
    array('124'=> 3) 
);

首先计算权重的总和,或者可能已经知道

$total_weight = 0;
foreach($group as $weight){
    $total_weight += $weight;
}

然后生成一个从 0 到 $total_weight 的随机数,例如。0<=$rand_number

$current_total = 0;
foreach($group as $name => $weight){
    if($rand_number <= $current_total)
        return $name;
    $current_total += $weight;
}

--

顺便说一句,我是新来的,要学习更多:)

于 2013-09-12T10:18:43.080 回答
1

检查这个:-

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

输出 :-

Array
(
    [0] => 122
    [1] => 123
    [2] => 123
    [3] => 123
    [4] => 123
    [5] => 124
    [6] => 124
    [7] => 124
)
于 2013-09-12T09:53:43.113 回答
1
<?php
$draw = array();
foreach($all_entrants as $entrant) {
    for($i=0; $i<$entrant['number_of_entries']; $i++) {
        $draw[] = $entrant['username'];
    }
}
于 2013-09-12T09:48:28.047 回答
1
$draw = array();
foreach($all_entrants as $entrant) {
    for($i=0; $i<$entrant['number_of_entries']; $i++) {
        $draw[] = $entrant['username'];
    }
}
于 2013-09-12T09:50:01.540 回答
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);
于 2013-09-12T10:03:55.300 回答
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);
?>

希望能帮助到你。

于 2013-09-12T09:52:06.663 回答
0

尝试这个

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

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

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

   }



       }
于 2013-09-12T09:50:19.257 回答