Ashley Ashley - 3 months ago 12
JSON Question

Extract data fron JSON array using loop

im trying to filter a JSON array response as i only need a small part of the results.

I need to get the players

displayName
only.

Here is the repose for the first player, there can be upto 12 player per match.

I need something that can loop through and extract the names..

[displayName] => jonhofun


At present the only way i can get the data i need is by doing

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


heres the original response

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


Any help would be appreciated.

Answer

You need to loop through the sub array under "entries".

foreach ($json11['Response']['data']['entries'] as $entries) {
    $player_names[] = $entries['player']['destinyUserInfo']['displayname'];
}

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