• 0

[PHP] Creating JSON array


Question

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

16 answers to this question

Recommended Posts

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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

  • 0

5zaRIpp.png

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

  • 0

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  :s

Link to comment
Share on other sites

  • 0

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

  • 0
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

  • 0
$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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.