使用PHP遍历多维数组

时间:2018-08-27 19:53:06

标签: php html arrays api

我正在尝试学习php并与API通讯,以获取我的Destiny 2家族的信息。我已经检索了所有需要的信息,并使用以下格式构建了一个新数组:

Array
(
    [displayname] => Senaxx
    [membershipId] => 4611686018428643772
    [characterdetails] => Array
        (
            [0] => Array
                (
                    [2305843009260824684] => Array
                        (
                            [characterlevel] => 30
                            [light] => 384
                            [datelastplayed] => 2018-08-19T13:23:27Z
                            [minutesplayed] => 14901
                            [racehash] => 898834093
                            [genderhash] => 3111576190
                            [emblempath] => /common/destiny2_content/icons/2b6160e0f21e748cd996b404771a850c.jpg
                            [emblemhash] => 10493725
                        )

                )

            [1] => Array
                (
                    [2305843009260824685] => Array
                        (
                            [characterlevel] => 30
                            [light] => 394
                            [datelastplayed] => 2018-08-18T20:47:09Z
                            [minutesplayed] => 9647
                            [racehash] => 2803282938
                            [genderhash] => 3111576190
                            [emblempath] => /common/destiny2_content/icons/dcfe74b7a343dd28bd45eee4ea59a1f7.jpg
                            [emblemhash] => 3860733295
                        )

                )

            [2] => Array
                (
                    [2305843009260824686] => Array
                        (
                            [characterlevel] => 30
                            [light] => 394
                            [datelastplayed] => 2018-08-26T00:15:50Z
                            [minutesplayed] => 1802
                            [racehash] => 2803282938
                            [genderhash] => 3111576190
                            [emblempath] => /common/destiny2_content/icons/dcfe74b7a343dd28bd45eee4ea59a1f7.jpg
                            [emblemhash] => 3860733295
                        )

                )

        )

)
Array
(
    [displayname] => base1981
    [membershipId] => 4611686018433367605
    [characterdetails] => Array
        (
            [0] => Array
                (
                    [2305843009265162076] => Array
                        (
                            [characterlevel] => 30
                            [light] => 358
                            [datelastplayed] => 2018-08-15T11:58:46Z
                            [minutesplayed] => 13732
                            [racehash] => 2803282938
                            [genderhash] => 3111576190
                            [emblempath] => /common/destiny2_content/icons/9d512efea06e54c1768d434e53510092.jpg
                            [emblemhash] => 1291068173
                        )

                )

            [1] => Array
                (
                    [2305843009265162077] => Array
                        (
                            [characterlevel] => 1
                            [light] => 100
                            [datelastplayed] => 2017-09-06T14:25:35Z
                            [minutesplayed] => 0
                            [racehash] => 898834093
                            [genderhash] => 2204441813
                            [emblempath] => /common/destiny2_content/icons/911791e90f955fc637398ea88aba74b7.jpg
                            [emblemhash] => 1907674137
                        )

                )

            [2] => Array
                (
                    [2305843009265162078] => Array
                        (
                            [characterlevel] => 30
                            [light] => 350
                            [datelastplayed] => 2018-05-27T12:07:38Z
                            [minutesplayed] => 1658
                            [racehash] => 3887404748
                            [genderhash] => 3111576190
                            [emblempath] => /common/destiny2_content/icons/da0d265bb9e4473c97ba56dc7602ca73.jpg
                            [emblemhash] => 3941202506
                        )

                )

        )

)

这些只是2个播放器阵列。我的整个代码为8。但是对于这个示例,我认为其中2个就足够了。

我的目标是遍历数组并检索每个显示名称的所有玩家信息。

所以我从最完美的开始:

foreach($claninfo as $clanmember)
{
 echo $clanmember['membershipId']; 
}

仅抓住角色的细节对我来说并不足够可悲。 对于mee来说,问题似乎是有一个额外的characterId和一个未知的数字。

foreach($claninfo as $clanmember)
{
 echo $clanmember['membershipId']; 
    foreach($claninfo->characterdetails as $characterdetails)
    {
      echo $characterdetails-> light;
    } 
}

最终目标是要在其中包含一些信息的表:

<table>
<tbody>
<tr>
<td>Displayname</td>
<td>Playtime</td>
<td>Character 1</td>
<td>Character 2</td>
<td>Character 3</td>
</tr>
<tr>
<td>Senaxx</td>
<td>&nbsp;</td>
<td>light 384</td>
<td>light 394</td>
<td>light 394</td>
</tr>
<tr>
<td>Base1981</td>
<td>&nbsp;</td>
<td>light 358</td>
<td>light 100</td>
<td>light&nbsp;350</td>
</tr>
</tbody>
</table>

4 个答案:

答案 0 :(得分:2)

直到亮起的关联数组键为: $ claninfo [<某些键或索引>] ['characterdetails'] [] [] ['light']

因此要访问 light ,您需要使用 foreach 3次:

  1. 从claninfo获取氏族成员

  2. 要迭代$ clanmember ['characterdetails']

  3. 摆脱字符ID

    foreach($claninfo as $clanmember)
    {
        echo $clanmember['membershipId']; 
    
    
       foreach($clanmember['characterdetails'] as $characterArray)
        {
    
           foreach($characterArray as $characterid => $characterdetails)
           { // You can access $characterid in this block
               echo $characterdetails['light'];
           } 
        } 
    }
    

答案 1 :(得分:1)

characterdetails是一个数组,而不是一个对象。

您可以使用以下代码遍历数组

foreach($claninfo as $clanmember)
{
    echo $clanmember['membershipId']; 

    foreach($claninfo['characterdetails'] as $characterdetails)
    {
        echo $characterdetails['light'];
    } 
}

答案 2 :(得分:1)

您的代码似乎存在3个不同的问题。您在处理数组时,根本不要使用对象符号(->)。您在内部foreach中使用了错误的数组。第三,这可能是您真正的问题,您需要一种方法来访问您不知道的密钥(未知ID)。例如,可以使用array_values (manual)来规避最后一个。在一起:

foreach($claninfo as $clanmember)
{
  echo $clanmember['membershipId']; 
  foreach($clanmember['characterdetails'] as $characterdetails) 
  {
    echo array_values($characterdetails)[0]['light'];
  } 
}

答案 3 :(得分:0)

我的最终解决方案是:

foreach($claninfo as $clanmember)
{
    echo '<br><b>' . $clanmember['displayname'] . '</b><br>' ; 

  foreach($clanmember['characterdetails'] as $characterArray)
  {
    $characterId = $characterArray['characterId'];
    $characterlight = $characterArray['light'];
    $characterlevel = $characterArray['characterlevel'];
    $characterlastplayed = $characterArray['datelastplayed'];
    $characterminutesplayed = $characterArray['minutesplayed'];
    $characterracehash = $characterArray['racehash'];
    $charactergenderhash = $characterArray['genderhash'];
    $characteremblempath = $characterArray['emblempath'];
    $characteremblemhash = $characterArray['emblemhash'];

    echo 'CharacterId: ' . ($characterId) . '<br>';
    echo 'Light: ' . ($characterlight) . '<br>';
    echo 'level: ' . ($characterlevel) . '<br>';
    echo 'Last Played: ' . ($characterlastplayed) . '<br>';
    echo 'Minutes Played: ' . ($characterminutesplayed) . '<br>';
    echo 'RaceHash: ' . ($characterracehash) . '<br>';
    echo 'GenderHash: ' . ($charactergenderhash) . '<br>';
    echo 'Emblempath: ' . ($characteremblempath) . '<br>';
    echo 'Emblemhash: ' . ($characteremblemhash) . '<br>';
    echo '<br>';
  } 
}