0

I have a JSONArray generated in java and I post it to one of my PHP files where it's saved to a file. From there it gets read and I need to generate a chart based on this data. All I need is to convert my raw JSON which has values I don't need, into a simply php array.

[{"id":1,"timestamp":"1363135091","reward":1200,"player":"Orangeguy24","address":"108.28.239.167","service":"MC-Index"},{"id":2,"timestamp":"1363135091","reward":1200,"player":"Orangeguy24","address":"108.28.239.167","service":"MC-Index"}]

Is an example of 2 elements inside my JSON array. What I need todo is filter those values into arrays accordingly.

For example get how many votes a 'player' has, I need to add up how ever many elements are in the JSONArray, because 1 element is 1 vote (the id is primary auto-increment in my mysql DB, not located on my webserver)

I'd like the array to be to [player, votes] so when I echo the array it will be easily parsed by the google chart tools I'm using. I've spent the last 5 hours working on this and I've been stuck, thanks for any help!

2
  • 1
    What have you tried? json_decode, for starters? Commented Mar 19, 2013 at 0:16
  • <?php $string = file_get_contents("../json/voting.txt"); $json = json_decode($string); echo $string; ?> I'm parsing milkycraft.net/json/voting.txt Commented Mar 19, 2013 at 0:21

2 Answers 2

1

To decode the JSON into a php array, you can do:

$json_array = json_decode($raw_json);

Then, to get the number of votes for each player out of the array:

$player_votes = array_reduce($json_array,
    function($v, $item) {
        if(!array_key_exists($item->player, $v))
            $v[$item->player] = 1;
        else
            $v[$item->player] = 1 + $v[$item->player];

        return $v;
    }, array());

If I understand your question correctly, this will work.

EDIT: Updated the second code snippet

Sign up to request clarification or add additional context in comments.

1 Comment

Vardump produces: array(1) { [""]=> int(337) }
0

Try this :

$str   = '[{"id":1,"timestamp":"1363135091","reward":1200,"player":"Orangeguy24","address":"108.28.239.167","service":"MC-Index"},{"id":2,"timestamp":"1363135091","reward":1200,"player":"Orangeguy24","address":"108.28.239.167","service":"MC-Index"}]';

$res   = array();
foreach(json_decode($str,true) as $val){
  if(array_key_exists($val['player'],$res)){
     $res[$val['player']]   = $res[$val['player']]+1;
  }
  else{
     $res[$val['player']]   = 1;
  }

}

echo "<pre>";
print_r($res);

Output :

Array
(
    [Orangeguy24] => 2
)

1 Comment

Thanks works well after I strtolower'd the entire json string, now I just need to figure how to get the array back into a format that google charts likes, it's pretty picky.

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.