多维数组,我该如何解决?

时间:2013-05-13 07:57:23

标签: php multidimensional-array foreach

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

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/>

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

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/>";
}

2 个答案:

答案 0 :(得分: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/>";
}

<强>解释

第一个循环将&#34;访问&#34;两个主要分支(SKILLSKILL1)。这些值(因为它们是数组键)被保存到$skillName变量中,而整个玩家数组将保留$players变量。
现在你必须遍历每个用户。为此,你写另一个foreach,其中键将保持玩家位置(好吧,它们是零索引,但如果你想要你可以添加一个单位),最后data将包含实际数据。

答案 1 :(得分: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>

查看: View