2013-05-13 56 views
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/> 

該陣列由 「獲得」 排序。
用戶排名$ loopRuns上漲$獲得

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

能否請您發佈'var_export(AR1 $)的輸出;'而不是print_r的結果呢?讓它更容易測試... – VolkerK 2013-05-13 08:10:50

回答

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將包含實際的數據。

+0

輸出:SKILL0加入581加入152加入2加入:SKILL20加入1381加入1272加入126加入 – user2311073 2013-05-13 08:04:44

+0

@ user2311073:我沒有使用任何格式樣式 – DonCallisto 2013-05-13 08:05:26

+0

@ user2311073:現在我已更新 – DonCallisto 2013-05-13 08:07:25

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