2

Basically idea is to grab data from MySQL table and transform it to JSON.

This is how database table looks:

enter image description here

And this how should output be:

[
   {"group1":[
               {"val":"somevalue"},
               {"val":"somevalue"}
             ]
   },
   {"group2":[
               {"val":"somevalue"},
               {"val":"somevalue"}
             ]
   },
   {"group3":[
               {"val":"somevalue"}
             ]
   }
]

My PHP script looks like this, for now:

$arr = [];
$result = mysql_query("SELECT * FROM thetable WHERE section='sect1'");
while($row = mysql_fetch_array($result))
{
  // ???
}

echo json_encode($arr);

My main issue is how to output/sort data in "groups".

Thanks for your help!

enloz
  • 5,704
  • 9
  • 37
  • 56
  • Check out this SO post: http://stackoverflow.com/questions/383631/json-encode-mysql-results – Bjoern Nov 23 '11 at 07:19

1 Answers1

3

try this

while($row = mysql_fetch_array($result))
{
   $arr[$row['group']][] = array('val' => $row['value']);
} 
k102
  • 7,861
  • 7
  • 49
  • 69