0

我一直在尝试解决这个问题,但我一直“获得”作为输出而不是用户。

array(2) 
{ 
    ["SKILL"]=> array(2) 
    { 
         [0]=> array(2)
         { 
            ["user"]=> string(7) "USERNAME" ["gained"]=> int(15) 
         } 
         [1]=> array(2) 
         {
             ["user"]=> string(6) "USERNAME" ["gained"]=> int(8) 
         } 
         [2]=> array(2) 
         {
              ["user"]=> string(6) "USERNAME" ["gained"]=> int(3) 
         } 

    ["SKILL2"]=> array(2) 
    { 
        [0]=> array(2)
        {  
            ["user"]=> string(7) "USERNAME" ["gained"]=> int(10) 
        } 
        [1]=> array(2) 
        {
            ["user"]=> string(6) "USERNAME" ["gained"]=> int(7) 
        } 
        [2]=> array(2) 
        {
           ["user"]=> string(6) "USERNAME" ["gained"]=> int(5) 
       } 
} 

我希望它输出:

USERNAME ranked 1 gained 15<br/>
USERNAME ranked 2 gained 8<br/>
USERNAME ranked 3 gained 3<br/>
<br/><br/>
["SKILL2"]:<br/>
USERNAME ranked 1 gained 10<br/>
USERNAME ranked 2 gained 7<br/>
USERNAME ranked 3 gained 5<br/>
<br/><br/>

数组按“ gain ”排序。
用户排名$loopRuns获得$ gained

foreach($ar1 as $skill => $value)
{
    $i = 0;
    echo "<b>Skill: $skill</b><br/>";

    foreach($value as $ar2 => $ar3)
    {
        foreach($ar3 as $key => $value)
        {
            $i++;
                echo "$key Ranked #$i gained $value<br/>";
        }
    }
    echo "<br/>";
}
4

2 回答 2

0

使用字段集和图例如何?

代码:

<?php

$skills = array(
    'SKILL' => array(
        array(
            'user'   => 'USERNAME',
            'gained' => 15        ,
        ),
        array(
            'user'   => 'USERNAME',
            'gained' => 8         ,
        ),
        array(
            'user'   => 'USERNAME',
            'gained' => 3         ,
        ),
    ),
    'SKILL2' => array(
        array(
            'user'   => 'USERNAME',
            'gained' => 10        ,
        ),
        array(
            'user'   => 'USERNAME',
            'gained' => 7         ,
        ),
        array(
            'user'   => 'USERNAME',
            'gained' => 5         ,
        ),
    ),
);


foreach ($skills as $skill => $users) {

    echo "<fieldset>\n";
    echo "<legend>{$skill}</legend>\n";
    foreach ($users as $key => $user) {
        printf("%s ranked %d gained %d<br />\n",
            $user['user']  ,
            $key + 1       ,
            $user['gained']
        );
    }
    echo "</fieldset>\n";

}

输出:

<fieldset>
<legend>SKILL</legend>
USERNAME ranked 1 gained 15<br />
USERNAME ranked 2 gained 8<br />
USERNAME ranked 3 gained 3<br />
</fieldset>
<fieldset>
<legend>SKILL2</legend>
USERNAME ranked 1 gained 10<br />
USERNAME ranked 2 gained 7<br />
USERNAME ranked 3 gained 5<br />
</fieldset>

看法: 看法

于 2013-05-13T08:13:57.797 回答
0
foreach($skillArray as $skillName => $players)
{
  echo "skill: ".$skillName."<br/>";
  foreach($players as $rank => $data)
  {
    echo $data["user"]." ranked ".$rank." gained ".$data["gained"]." <br/>";
  }
  echo "<br/>";
}

解释

第一个循环将“访问”两个主要分支(SKILLSKILL1)。这些值(因为它们是数组键)保存在$skillName变量中,而整个播放器数组将由$players变量保存。
现在您必须遍历每个用户。为此,您编写另一个foreach键将保持玩家位置的位置(好的,它们是零索引,但如果您愿意,可以向其中添加一个单元),最后data将包含实际数据。

于 2013-05-13T08:02:06.003 回答