使用循环从JSON数组中提取数据

时间:2016-08-20 15:30:32

标签: php arrays json sorting

我试图过滤JSON数组响应,因为我只需要一小部分结果。

我只需要让玩家displayName

这是第一个玩家的休息,每场比赛最多可以有12个玩家。

我需要能够循环并提取名称的东西。

[displayName] => jonhofun

目前我能获得所需数据的唯一方法是做

$player1 = $json11['Response']['data']['entries']['0']['player']['destinyUserInfo']['displayName'];
$player2 = $json11['Response']['data']['entries']['1']['player']['destinyUserInfo']['displayName'];
etc... etc...

继承人最初的回应

Array
(
[Response] => Array
    (
        [data] => Array
            (
                [period] => 2016-08-20T10:16:46Z
                [activityDetails] => Array
                    (
                        [referenceId] => 3156370656
                        [instanceId] => 5370359303
                        [mode] => 12
                        [activityTypeHashOverride] => 3614615911
                    )

                [entries] => Array
                    (
                        [0] => Array
                            (
                                [standing] => 0
                                [score] => Array
                                    (
                                        [basic] => Array
                                            (
                                                [value] => 2190
                                                [displayValue] => 2,190
                                            )

                                    )

                                [player] => Array
                                    (
                                        [destinyUserInfo] => Array
                                            (
                                                [iconPath] => /common/destiny_content/icons/d0d3cd4c26aa1a931d46c4bf720856ba.jpg
                                                [membershipType] => 2
                                                [membershipId] => 4611686018454971653
                                                [displayName] => jonhofun
                                            )

                                        [characterClass] => Warlock
                                        [characterLevel] => 40
                                        [lightLevel] => 322
                                    )
                            )
                    )
            )
    )
)

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

您需要在"条目"。

下循环子数组
foreach ($json11['Response']['data']['entries'] as $entries) {
    $player_names[] = $entries['player']['destinyUserInfo']['displayname'];
}

echo "<pre>";
print_r($player_names);    // Check all player names

答案 1 :(得分:-1)

使用for循环可以很容易地实现这一点,请查看以下示例,该示例可以让您了解如何继续:

// totalPlayers will need to be changed to the total number of entries you have
for ($x = 1; $x < $totalPlayers; $x++) {
    // You will need to do something with the nextPlayer here before the next iteration of the loop
    $nextPlayer = $json11['Response']['data']['entries'][$x]['player']['destinyUserInfo']['displayName'];
}

请注意,返回数据中的硬编码播放器标识符将替换为$x循环中的计数器for