hyster Posted January 13, 2014 Share Posted January 13, 2014 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, }, ] } } Quote Link to comment https://forums.phpfreaks.com/topic/285328-splitting-json/ Share on other sites More sharing options...
gizmola Posted January 13, 2014 Share Posted January 13, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285328-splitting-json/#findComment-1465071 Share on other sites More sharing options...
hyster Posted January 13, 2014 Author Share Posted January 13, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/285328-splitting-json/#findComment-1465124 Share on other sites More sharing options...
Solution requinix Posted January 13, 2014 Solution Share Posted January 13, 2014 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 } Quote Link to comment https://forums.phpfreaks.com/topic/285328-splitting-json/#findComment-1465128 Share on other sites More sharing options...
hyster Posted January 14, 2014 Author Share Posted January 14, 2014 thanks requinix, that was enough to get more sorted after an hour of messing about lol Quote Link to comment https://forums.phpfreaks.com/topic/285328-splitting-json/#findComment-1465145 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.