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!

1

1 Answer 1

3

try this

while($row = mysql_fetch_array($result))
{
   $arr[$row['group']][] = array('val' => $row['value']);
} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.