I´m trying to create my own leaderboards sytem for my games so I´m working with PHP and requesting info with Ajax into the games, but as I´m not good at all with PHP I´m pretty confused about how to create a JSON object with all the info I need to handle in the javascript part.
What I want to do, in the PHP part is to generate this JSON object:
{players: ["name1", "name2", ..., "name10"], scores:[score1, score2, ..., score10]}
So I can work in javascript with something like
dataReceived.players[0]
I´m storing and getting the data correctly from the database but I´m not being able to generate that JSON object to receive in the Ajax request. Basically, this is my PHP code:
$query = "SELECT * FROM leadersboards ORDER by score ASC LIMIT 10";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$result_length = mysql_num_rows($result);
$arrayScores = array();
$arrayPlayers = array();
for($i = 0; $i < $result_length; $i++){
$row = mysql_fetch_array($result);
array_push($arrayPlayers, $row['player']);
array_push($arrayScores, $row['score']);
}
$answer = json_encode(array('item' => $arrayPlayers, 'item' => $arrayScores), JSON_FORCE_OBJECT);
Sorry if I made something stupid in PHP, as I said, I don´t know PHP at all, just pretty basic stuff.