I've got a PHP array and echo that into javascript with json encode, i need to do it this way because it's going to be very dynamic. This is the code it echo's:

{"notempty":true}

And i use this to, convert it to javascript:

var myarray = eval('(' + json + ')');

For some reason it creates an object instead of an array and for that reason i cant use .length or a for loop.

Does someone know what im doing wrong here?

Thanks

link|improve this question

50% accept rate
In JS enumerated array (or what would be an enumerated array in PHP) behaves like object – Mchl Aug 17 '11 at 16:34
The reason is that you cannot have associative arrays in JavaScript. Objects are the closest match. – Álvaro G. Vicario Aug 17 '11 at 16:35
Do not use eval. Use a JSON parse method like jQuery's api.jquery.com/jQuery.parseJSON or use Douglas Crockford's parseJSON method. See github.com/douglascrockford/JSON-js (download json2.js) – nickyt Aug 17 '11 at 16:40
feedback

4 Answers

up vote 3 down vote accepted

You're trying to treat an Object like an Array, and an Object is not an Array, it is an Object.

Any time you see {} in JSON, that means "What is contained within these hallowed brackets is a dynamic object". When you see [], that means "Behold! I am an Array" (there are notable exceptions to this one: jQuery does some special work with to make itself look like an array).

So, in order to iterate through an Object, you'll want to use for... in.

// eval BAD unless you know your input has been sanitized!.
var myObj = JSON.parse('{"notempty":true}');
// personally, I use it in for... in loops. It clarifies that this is a string
// you may want to use hasOwnProperty here as sometimes other "keys" are inserted
for( var it in myObj ) console.log( "myObj["+it+"] = " + myObj[it] );
link|improve this answer
1  
Don't forget to use hasOwnProperty when iterating over an objects properties: for (var it in myObj) if(myObj.hasOwnProperty(it)) {do something} – Dan Manastireanu Aug 17 '11 at 16:45
Thank you very much, telling me the way to loop through an object totally solved my problem. – Chris Aug 17 '11 at 17:15
feedback

{} is an object, which contains one attribute named notempty. If you want an array, it'd have to be

[{"notempty":true}]

which is an array with a single element at index 0, which is an object with the single attribute 'notempty';.

link|improve this answer
feedback

By default, if you use encode an assoc array in php, it will become a js object when you decode. In order to have it be an array, you need to make it an array in php:

PHP:

$arr = "['notempty','notempty2','notempty3']";

Otherwise, you should convert it to an array in JS, but that seems to me a waste since looping through the object in javascript is so much easier:

Javascript:

var arr = new Array();
for(var i in obj) arr[i] = obj[i];
link|improve this answer
feedback

You can use jQuery to parse it into an array like this:

var p = [];
$.each(jsonData, function (key, val) {
    p.push([val.propertyOne, val.propertyTwo]);
});

I am presuming of course that you want to parse JSON, not an array or any other string.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.