3

This is a little bit of a hard problem to explain, so ill show you instead. If you look below you will see valid JSON.

{
    "data":{
        "0":{
            "action_id":"1",
            "date":"2012-04-10 15:07:38",
            "action_type":"1",
            "action_text":"Some one got blamed!"
        },
        "1":{
            "action_id":"2",
            "date":"2012-04-10 16:18:05",
            "action_type":"1",
            "action_text":"Testing multiple items for AJAX"
        },
        "total":2,
        "ajax_message":"Success",
        "ajax_status":"0",
        "success":"true"
    }
}

But for the application were using it cannot handle the "0": ,"1", it instead just wants it comma separated .

My current code to generate this is:

while ($r = mysql_fetch_assoc($q)) {
        $array[] = $r;
    }
json_encode($array);

Fairly simple and raw stuff at the moment. But i think i may have to write a json_encode for myself in order for it to print it like this.....

Any help would be greatly appreciated

NOTE: This is a valid form(written by hand):

{
"data": [
{
"action_id": "1",
"date": "2012-04-10 15:07:38",
"action_type": "1",
"action_text": "Some one got blamed!",
"fb_id": "760775384"
},
{
"action_id": "2",
"date": "2012-04-10 16:18:05",
"action_type": "1",
"action_text": "Testing multiple items for AJAX",
"fb_id": "760775384"
}
],
"total": 2,
"ajax_message": "Success",
"ajax_status": "0",
"success": "true"
}

3 Answers 3

3

The problem is that your numeric keys (0, 1) are at the same level in the hash as named keys (total, ajax_message, etc). Instead of doing this:

$a = array();
while ($r = mysql_fetch_assoc($q)){
    $a[] = $r;
}
$a['key'] = value;

Do this:

$a = array();
$a['rows'] = array();
while ($r = mysql_fetch_assoc($q)){
    $a['rows'][] = $r;
}
$a['key'] = value;

If every key in the array ($a['rows'] in this example) is numeric, json_encode() will output it as a [{list}, {like}, {this}]

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much that worked excellently and your explanation made it alot easier to understand where i screwed up. I was unaware of PHP's behavior with this. Do you happen to know if this was intentional or a by product of how they created json_encode?
This is how JSON encoding should work - it's fallout form the way PHP doesn't treat associative-arrays (hashs) any different from traditional arrays, unlike JavaScript. json_encode() tries to guess if you meant to have a regular array or an object, by looking at the keys.
1

You need an extra level of array.

You've got:

array( 
  'data' => array(
    '0' => array()
    '1' => array()
    'total' => 2,
    'etc' => 'blah'
  )
)

But you're asking for this:

array( 
  'data' => array(
    array(),
    array()
  )
  'total' => 2,
  'etc' => 'blah'
)

As Cal says, the deeper issue is that you've got (implicit) numeric keys mixed in with string keys of the array you're encoding. That's fine in PHP, but it's not valid for arrays in JSON, only objects.

json_encode will encode arrays without string keys as JSON arrays, but it needs to encode string keyed arrays as objects for them them to be valid JSON.

1 Comment

So if i just do something like $array[][] it would interpret it correctly?
0
while ($r = mysql_fetch_assoc($q)) {
    $array[][] = $r;
}
json_encode($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.