Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:循环遍历多维PHP数组_Php_Multidimensional Array - Fatal编程技术网

PHP:循环遍历多维PHP数组

PHP:循环遍历多维PHP数组,php,multidimensional-array,Php,Multidimensional 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), ... ) 根据这些条目,我需要创建另一个名

我有一组“参赛者”,可以这样显示:

 $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
的数组。
$draw
数组的
用户名
重复次数与其对应的
条目数
相同。因此,对于上面的示例,它可能如下所示:

 $draw = array("122", "123", "123", "123", "123", "124", "124", "124")
我想这样做,以便我以后可以生成一个随机数,并通过执行类似于
$draw[$randomNumber]的操作来找到赢家


然而,我无法理解如何从
$all\u entrants
数组中创建
$draw
数组。。。任何帮助都将不胜感激

我想你是在找这样的东西吧

$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
$draw=array();
foreach($entract作为$entract的所有参与者)//使用参与者在数组中循环

对于($i=0;$i我想你在找这样的东西

$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
$draw=array();
foreach($entract作为$entract的所有参与者)//使用参与者在数组中循环
对于($i=0;$i
$draw=array();
foreach(所有参赛者均为$entrant){
对于($i=0;$i
$draw=array();
foreach(所有参赛者均为$entrant){
对于($i=0;$i试试这个

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

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

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

   }



       }
$newarray=array();
foreach(所有参赛者作为$list){
对于($i=1;$i试试这个

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

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

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

   }



       }
$newarray=array();
foreach(所有参赛者作为$list){
对于($i=1;$i

希望能有所帮助。


希望有帮助。

检查以下内容:--

选中此项:--


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

$total_weight = 0;
foreach($group as $weight){
    $total_weight += $weight;
}
首先计算重量之和,或者可能已经知道

$current_total = 0;
foreach($group as $name => $weight){
    if($rand_number <= $current_total)
        return $name;
    $current_total += $weight;
}
然后生成一个从0到$total_weight的随机数,例如0$weight){
如果($rand_number我认为这是一个关于从一组具有不同权重的名称中选择一个的问题。 也许是这样的数组

$total_weight = 0;
foreach($group as $weight){
    $total_weight += $weight;
}
首先计算重量之和,或者可能已经知道

$current_total = 0;
foreach($group as $name => $weight){
    if($rand_number <= $current_total)
        return $name;
    $current_total += $weight;
}
然后生成一个从0到$total_weight的随机数,例如0$weight){
如果($rand_number@BenFortune你是对的,谢谢,马上修好!谢谢!试过了,这正是我需要的!几分钟后会标记为正确。@BenFortune你是对的,谢谢,马上修好!谢谢!试过了,这正是我需要的!几分钟后会标记为正确。所有答案中最好的。最好的答案在所有这些答案中。
$group = array(
    array('122' => 1),
    array('123'=> 4),
    array('124'=> 3) 
);
$total_weight = 0;
foreach($group as $weight){
    $total_weight += $weight;
}
$current_total = 0;
foreach($group as $name => $weight){
    if($rand_number <= $current_total)
        return $name;
    $current_total += $weight;
}