9

I am trying to json_encode an array which is returned from a Zend_DB query.

var_dump gives: (Manually adding 0 member does not change the picture.)

array(3) {
  [1]=>
  array(3) {
    ["comment_id"]=>
    string(1) "1"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "test 1"
  }
  [2]=>
  array(3) {
    ["comment_id"]=>
    string(1) "2"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "test 1"
  }
  [3]=>
  array(3) {
    ["comment_id"]=>
    string(1) "3"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "jhghjg"
  }
}

The encoded string looks like:

{"1":{"comment_id":"1","erasable":"1","comment":"test 1"},
 "2":{"comment_id":"2","erasable":"1","comment":"test 1"},
 "3":{"comment_id":"3","erasable":"1","comment":"jhghjg"}}

What I need is:

[{"comment_id":"1","erasable":"1","comment":"test 1"},
{"comment_id":"2","erasable":"1","comment":"test 1"},
{"comment_id":"3","erasable":"1","comment":"jhghjg"}]

Which is what the php.ini/json_encode documentation says it should look like.

4 Answers 4

13

How are you setting up your initial array?

If you set it up like:

array(
 "1" => array(...),
 "2" => array(...),
);

then you don't have an array with numeric indexes but strings, and that's converted to an object in JS world. This can happen also if you don't set a strict order (i.e. starting at 0 instead of 1).

This is a shot in the dark, however, because I can't see your original code: try setting your array without using keys at all in the first place:

array(
 array(...),
 array(...),
);
5
  • 2
    +1 # an array not starting with 0 for its key isn't an array. phps fault though for having a weird bastard lovechild of array/hash that works properly as neither. Commented Mar 26, 2009 at 2:22
  • Like I said, that doesn't make it an array, that makes it a hash with a numeric key, which just happens in this case to be close to 0 at the start because thats what the data in the database has. Commented Mar 26, 2009 at 2:42
  • Translation: what you want is logically impossible. Commented Mar 26, 2009 at 2:43
  • 1
    +1 to Kent... wish I could upvote your comments... instead, I upvoted your answer :) Commented Mar 26, 2009 at 12:12
  • @Kent, I also can't stand that PHP treats hashes and arrays as the same thing. That's also been the reason for many people using map = new Array(); map.prop = 'blah' in JS. They are not the same! Commented Jan 7, 2011 at 20:07
6

Added information that expands on Seb's answer.

php > print json_encode( array( 'a', 'b', 'c' ) ) ;
["a","b","c"]
php > print json_encode( array( 0 => 'a',  1 => 'b', 2 => 'c' ) ) ;
["a","b","c"]
php > print json_encode( array( 1 => 'a',  2 => 'b', 3 => 'c' ) ) ;
{"1":"a","2":"b","3":"c"}
php > 

Note: its formatting it this way with good cause:

If you were to send

{"1":"a","2":"b","3":"c"}

as

["a","b","c"]

When you did $data[1] in Php you would get back "a", but on the JavaScript side, you would get back "b" .

2

A common way to test for a traditional, continuous array in php is to check for an index '0'. Try adding that to your array, it'll probably considering it an array instead of hashmap.

-3

i had a similar problem, got it to work after adding '' (single quotes) around the json_encode string. Following from my js file:

var myJsVar =  <?php echo json_encode($var); ?> ;    -------> NOT WORKING  
var myJsVar = '<?php echo json_encode($var); ?>' ;   -------> WORKING
1
  • 1
    That makes absolutely no sense. It will expand to var myJsVar = '{"1":"a"....}' which is a string, not an array. You'll have to decode it somehow in JS Land to make it usable, either by handing it off to a proper JSON parser or doing myJsVar = eval(myJsVar) somewhere later. Commented Feb 6, 2011 at 12:27

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.