DPyro Posted April 6, 2015 Share Posted April 6, 2015 I'm sure this is something simple I'm missing, but I can't seem to get the right output I'm looking for. $output['data']= array( "character_id" => $id, "name" => $name, "score" => $score, "time_played" => $time_played ); echo json_encode($output); Result looks like: { "data":{ "character_id":"5428010618035589553", "name":"Player1", "score":"221332661", "time_played":"176 Days 14 Hours 31 Minutes" } }{"data":{"character_id":"5428019223852978081","name":"Player2","score":"216120181","time_played":"198 Days 3 Hours 15 Minutes"}} What I want: { "data":[ { "character_id":"5428010618035589553", "name":"Player1", "score":"221332661", "time_played":"176 Days 14 Hours 31 Minutes" }, { "character_id":"5428019223852978081", "name":"Player2", "score":"216120181", "time_played":"198 Days 3 Hours 15 Minutes" } ] } Link to comment Share on other sites More sharing options...
0 SinsOfCube Posted April 6, 2015 Share Posted April 6, 2015 I'll presume the character data is fetched from a database and added dynamicly. You'll have to define the output['data'] as an array and keep pushing the new arrays with the character data in. //Needs to be defined outside the loop, otherwise you'll keep resetting $output['data'] $output['data'] = array(); while( $something is pulled from a database ) { //$output['data'][] creates a new entry in the array $output['data'][] = array( "character_id" => $id, "name" => $name, "score" => $score, "time_played" => $time_played ); } //Should now output the desired json echo json_encode( $output ); Link to comment Share on other sites More sharing options...
0 greenwizard88 Posted April 6, 2015 Share Posted April 6, 2015 You want an object, with data being a parameter. Something like ..what SinsofCube beat me to. Link to comment Share on other sites More sharing options...
0 firey Posted April 6, 2015 Share Posted April 6, 2015 Was beaten to the post. but yea.. you basically need to make your array.. an array. Link to comment Share on other sites More sharing options...
0 DPyro Posted April 6, 2015 Author Share Posted April 6, 2015 Ya data is fetched from daybreakgames api using foreach. Now getting the following: [ [ { "character_id":"5428010618035589553", "name":"Player1", "score":"221332661", "time_played":"176 Days 14 Hours 31 Minutes" }, { "character_id":"5428019223852978081", "name":"Player2", "score":"216120181", "time_played":"198 Days 3 Hours 15 Minutes" } ] ] I'd like the players to be in 'data' array object ->"data" ->->array ->->->"player1" ->->->"player2" Link to comment Share on other sites More sharing options...
0 firey Posted April 6, 2015 Share Posted April 6, 2015 You could try passing the JSON_FORCE_OBJECT as the second parameter in the encode. Link to comment Share on other sites More sharing options...
0 DPyro Posted April 6, 2015 Author Share Posted April 6, 2015 Heh nevermind, I had array_values set in the encode. EDIT: Works good. Now all I need is to look up the individual players for kill/death stats Link to comment Share on other sites More sharing options...
0 DPyro Posted April 6, 2015 Author Share Posted April 6, 2015 How would I get the child objects? { "characters_stat_history_list":[ { "stat_name":"deaths", "all_time":"5091" }, { "stat_name":"kills", "all_time":"15827" }, { "stat_name":"score", "all_time":"25025749" } ], "returned":3 } Tried this and it's not working foreach ($json->characters_stat_history_list as $player_stats) { $deaths=$player_stats[0][all_time]; $kills=$player_stats[1][all_time]; } Link to comment Share on other sites More sharing options...
0 Seahorsepip Veteran Posted April 6, 2015 Veteran Share Posted April 6, 2015 How would I get the child objects? {"characters_stat_history_list":[{"stat_name":"deaths","all_time":"5091"},{"stat_name":"kills","all_time":"15827"},{"stat_name":"score","all_time":"25025749"}],"returned":3} If it's a json string which I assume you'll have to parse the json string into an object. var objectWithObjects = $.parseJSON(jsonstring); var deaths; for(x=0;x<objectWithObjects["characters_stat_history_list"].length;x++) { if(objectWithObjects["characters_stat_history_list"][x]["stat_name"] == "deaths") { deaths = objectWithObjects["characters_stat_history_list"][x]["all_time"]; } } alert("all time deaths: "+deaths); Link to comment Share on other sites More sharing options...
0 DPyro Posted April 6, 2015 Author Share Posted April 6, 2015 Link to comment Share on other sites More sharing options...
0 Seahorsepip Veteran Posted April 6, 2015 Veteran Share Posted April 6, 2015 If it's a json string in php you should use json_encode to change the string into an object. And try $json["characters_stat_history_list"] Instead of $json->characters_stat_history_list Link to comment Share on other sites More sharing options...
0 DPyro Posted April 6, 2015 Author Share Posted April 6, 2015 I dunno, doesn't seem to work. Only thing I've gotten to return a value is $deaths=$player_stats->all_time[0]; $kills=$player_stats->all_time[1]; But they're obviously not the right values. According to this it should be characters_stat_history_list[0].all_time Link to comment Share on other sites More sharing options...
0 DPyro Posted April 7, 2015 Author Share Posted April 7, 2015 I'm stumped. Nothing I've tried is working. Link to comment Share on other sites More sharing options...
0 SinsOfCube Posted April 7, 2015 Share Posted April 7, 2015 You should try print_r on the decoded json object, so you can see the full contents of the object. This way you may find out what's going wrong. echo "<pre>"; print_r( $json ); echo "</pre>"; Link to comment Share on other sites More sharing options...
0 DPyro Posted April 7, 2015 Author Share Posted April 7, 2015 stdClass Object ( [characters_stat_history_list] => Array ( [0] => stdClass Object ( [stat_name] => deaths [all_time] => 15716 ) [1] => stdClass Object ( [stat_name] => kills [all_time] => 383607 ) [2] => stdClass Object ( [stat_name] => score [all_time] => 221467298 ) ) [returned] => 3 ) Link to comment Share on other sites More sharing options...
0 SinsOfCube Posted April 7, 2015 Share Posted April 7, 2015 (edited) $json['characters_stat_history_list'][0]['all_time']//Deaths $json['characters_stat_history_list'][1]['all_time']//Kills $json['characters_stat_history_list'][2]['all_time']//Score //Edit You should also set the second parameter of json_decode to true so you'll have json object converted to associative arrays, this will make it easier to go through it. json_decode($json_string, true ); Otherwise the above won't work I think Link to comment Share on other sites More sharing options...
0 DPyro Posted April 7, 2015 Author Share Posted April 7, 2015 Working now...could have sworn I tried what you posted already. Maybe was missing semi-colin Link to comment Share on other sites More sharing options...
Question
DPyro
I'm sure this is something simple I'm missing, but I can't seem to get the right output I'm looking for.
Result looks like:
What I want:
Link to comment
Share on other sites
16 answers to this question
Recommended Posts