Jump to content

splitting json


hyster

Recommended Posts

im trying to split a json dataset, the code below works on a different url but I think im stuck on an array in an array

cut down version of the json array is below the code.

any help / pointers gratefully accepted

<table border="1">
<?php
 
$jsonurl = "https://api.worldoftanks.eu/wot/account/tanks/?application_id=demo&account_id=502438129";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);


foreach ( $json_output->data as $data )
{
    echo "<tr><td>{$data->achievements->medal_dumitru}\n</td></tr>";
}
?>
</table>
{
    "status": "ok",
    "count": 1,
    "data": {
        "502438129": [
            {  // section repated 200 odd times
                "achievements": {
                    "medal_dumitru": 0,
                },
                "statistics": {
                    "clan": {
                    },
                    "all": {
                        "spotted": 0,
                    },
                    "max_xp": 0,
                    "wins": 418,
                    "company": {
                        "spotted": 0,
                    },
                    "battles": 663,
                },
                "last_battle_time": 0,
            },
        ]
     }
 }
Link to comment
https://forums.phpfreaks.com/topic/285328-splitting-json/
Share on other sites

json_decode has an option to convert the decoded output into an array rather than a series of objects.  This is often preferable in a scenario like this, where you have a deeply nested structure.

 

Add true as a second param:

 

 

$json_output = json_decode($json, true);
var_dump($json_output);

 

Now you will get a dump of the array and it should be a lot easier to see how to get down to the elements you want either by direct array index manipulation or nested foreach() blocks.

Link to comment
https://forums.phpfreaks.com/topic/285328-splitting-json/#findComment-1465071
Share on other sites

I have the list of data from the site im pulling it from, my aim is to pull it from the json and then put it into a local database. I can do it once I get the dataset split into $vars.

 

the json data I posted in my 1st post removed all but the 1st returned data

Link to comment
https://forums.phpfreaks.com/topic/285328-splitting-json/#findComment-1465124
Share on other sites

There's another layer in there you aren't accounting for:

$json_output = { status: ok, ...}
$json_output->data = { 502438129: [...] }
$json_output->data->{"502438129"} = [ { ... } ]
$json_output->data->{"502438129"}[0] = { achievements: ... }
$json_output->data->{"502438129"}[0]->achievements = { medal_dumitru: 0 }
Link to comment
https://forums.phpfreaks.com/topic/285328-splitting-json/#findComment-1465128
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.