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
You’re trying to treat an
Objectlike anArray, and anObjectis not anArray, it is anObject.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 usefor... in.