0

Hi i am working on a simple script where i would like to output the result of a given SQL query into JSON.

This is what i have so far:

$player = $this->game->getPlayer(5);
$leaderboard= $this->game->getLeaderboard(5);

$data = array(
            'player' => array(
                $player[0]->member_userunique, // USERNAME
                $player[0]->score, // HIGHSCORE
                $player[0]->memberid
            ),
            'leaderboard' => array(
                'score #1',
                'score #2',
                'score #3',
                'score #4',
                'score #5'
            )
        );
echo json_encode($data);

getPlayer(x) will return some information about the player. While getLeaderboard will get the top 5 records from the database. However, i would like to iterate each rows from the returned SQL result into the 'leaderboard' array.

I suppose i could use some brute-force by typing $leaderboard[0]->.... and so on but i would like to know the smarter way to do this.

Thanks for the help.

1
  • please show us the code how you fill $this->game ... or tell us how how we can know how many leaderboards there are. Also is the 5 in getLeaderboard(5) the number of items to return, or is it the identifier op the player ?
    – nl-x
    Commented May 31, 2013 at 9:19

3 Answers 3

2

Something like the below can be used to build up your array:

$leaderboardArray = array();
foreach($leaderboard as $num => $score){
    $leaderboardArray['score #'.$num] = $score; 
}
$data['leaderboard'] = $leaderboardArray;
1
  • thx for the hint, i took your solution and modify it a bit as needed.
    – Jeremy
    Commented May 31, 2013 at 9:13
0

Just loop in your $leaderboard array and build your array to include later

$player = $this->game->getPlayer(5);
$leaderboard= $this->game->getLeaderboard(5);

$leaderboard_array = array('leaderboard'=>array());
foreach($leaderboard as $val)
{
    $leaderboard_array[] = 'score #' . $val;
}

$data = array(
        'player' => array(
            $player[0]->member_userunique, // USERNAME
            $player[0]->score, // HIGHSCORE
            $player[0]->memberid
        ),
        $leaderboard_array,
    );
0

Well, you could just let getLeaderboard return the array with the scores like you want it and then insert it into $data like this

$data['leaderboard'] = $leaderboard;

The second solution is looping the leaderboard array adding each element (Assuming the leaderboard is sorted in the order you want when returned from getLeaderboard)

foreach($leaderboard as $entry) {
    $data['leaderboard'][] = "Score #" . $entry;
}

This assumes the leaderboard returns an array of strings or numbers and that $data['leaderboard'] is empty when you do it. If it's not empty either unset it or use keys to reference the position you're updating

1
  • yup basically that's what i need. Thx for the help.
    – Jeremy
    Commented May 31, 2013 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.