-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!

4
  • So you want us to tell you to wrap your JSON with []? :) Commented Jul 9, 2013 at 14:40
  • Please include a simple example: how the output looks now, how should look. The output from the links is unreadable. Commented Jul 9, 2013 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? Commented Jul 9, 2013 at 14:42
  • You want this: stackoverflow.com/questions/7097374/… Commented Jul 9, 2013 at 14:46

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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;

8 Comments

but why the hell the key is the same as the value? shouldnt it be array('subject'=>$data['subject']);
@steven only the op would know for sure..
Your answer does not reformat the json as the OP has asked.
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.
Updated to reformat as op requested
|
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.

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.