Jump to content

splitting json


hyster
Go to solution Solved by requinix,

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
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
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
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.