-1

Firstly, please take a look this JSON http://ws.luyencong.net/data/search/query.php?do=json

Here is it's code:

/* --- Execute query and get the data. --- */
$query = $db->query("SELECT t.* FROM ".TABLE_PREFIX."threads t");
while($data = $db->fetch_array($query))
{
    $results[] = array($data['subject'] => $data['subject']);
    $json = json_encode($results, JSON_FORCE_OBJECT);
}

/* --- Print the results to the screen, for future purposes. --- */
echo $json;

But I want the output JSON format will look like this: http://ws.luyencong.net/data/search/json.txt

Help will be appreciated. Thanks for anything.

Have a good day!

LCTG
  • 250
  • 1
  • 3
  • 14
  • So you want us to tell you to wrap your JSON with []? :) – N.B. Jul 09 '13 at 14:40
  • Please include a simple example: how the output looks now, how should look. The output from the links is unreadable. – matino Jul 09 '13 at 14:40
  • 1
    you want a json hash which has keys like `B\u1ed1 c\u00e1o th\u00e0nh l\u1eadp di\u1ec5n \u0111\u00e0n Luy\u1ec7n C\u00f4ng`? – miah Jul 09 '13 at 14:42
  • You want this: http://stackoverflow.com/questions/7097374/php-pretty-print-json-encode – Anil Jul 09 '13 at 14:46

3 Answers3

4

I believe you just want to change this:

$results[] = array($data['subject'] => $data['subject']);

To this:

$results[$data['subject']] = $data['subject'];

And, as @Orangepill suggested, move your json_encode call out of the loop. So your entire solution would look like this:

/* --- Execute query and get the data. --- */
$query = $db->query("SELECT t.* FROM ".TABLE_PREFIX."threads t");
$results = array();
while($data = $db->fetch_array($query))
{
    $results[$data['subject']] = $data['subject'];
}

/* --- Print the results to the screen, for future purposes. --- */
echo json_encode($results, JSON_FORCE_OBJECT);
Travesty3
  • 14,351
  • 6
  • 61
  • 98
2

you have to move your json_encode call outside of the loop.

/* --- Execute query and get the data. --- */
$query = $db->query("SELECT t.* FROM ".TABLE_PREFIX."threads t");
while($data = $db->fetch_array($query))
{
    $results[$data['subject']] = $data['subject'];
}
$json = json_encode($results, JSON_FORCE_OBJECT);
/* --- Print the results to the screen, for future purposes. --- */
echo $json;
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • but why the hell the key is the same as the value? shouldnt it be array('subject'=>$data['subject']); – steven Jul 09 '13 at 14:42
  • @steven only the op would know for sure.. – Orangepill Jul 09 '13 at 14:47
  • 1
    Your answer does not reformat the json as the OP has asked. – Schleis Jul 09 '13 at 14:48
  • This will make the code more efficient but as the `$json` variable gets overwritten every time in the loop, the end-result will be exactly the same. – jeroen Jul 09 '13 at 14:49
  • Updated to reformat as op requested – Orangepill Jul 09 '13 at 14:51
  • @jeroen $json assignment is out side of the loop...$json is not reassigned. – Orangepill Jul 09 '13 at 14:53
  • I was comparing your previous version to the OP's version where the variable gets overwritten and ends up being the same as yours in the last iteration of the loop... – jeroen Jul 09 '13 at 14:55
  • @steven : Because all datas in this JSON are thread titles in my forum, then now I want to add auto-suggestion to the forum so the key must be same as the value because of the jQuery plugin I'm using will need them for sure... – LCTG Jul 09 '13 at 15:05
1

You are nesting your arrays. It looks like you only want to have a single JSON object rather than an array of them. So change your setting of the $result to be:

$results[$data['subject']] = $data['subject'];

Then as suggested move the json_encode outside of the loop, you don't need to do that until after the array has been populated. You are just overwriting the variable over and over again for no reason.

Schleis
  • 41,516
  • 7
  • 68
  • 87