Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
1  
What have you tried? json_decode, for starters? –  fab Mar 19 '13 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 –  milkywayz Mar 19 '13 at 0:21
add comment

2 Answers

up vote 1 down vote accepted

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

share|improve this answer
    
Vardump produces: array(1) { [""]=> int(337) } –  milkywayz Mar 19 '13 at 0:27
    
I updated that code snippet accordingly. –  Connor McArthur Mar 19 '13 at 1:22
add comment

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
)
share|improve this answer
    
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. –  milkywayz Mar 19 '13 at 12:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.