I am trying to create an array in javascript which will allow me to access data like this:
var name = infArray[0]['name'];
however I cant seem to get anything to work in this way. When i passed out a assoc array from php to javascript using json_encode it structured the data in this way. The reason why i have done this is so i can pass back the data in the same format to php to execute an update sql request.
JavaScript doesn't have assoc arrays. Anything to any object declared as
obj['somthing']
is equal toobj.something
- and it is a property. Moreover in arrays it can be a bit misleading, so any added property won't changed array set tryobj.length
.JavaScript doesn't have associative arrays. It has (numeric) arrays and objects.
What you want is a mix of both. Something like this:
Then you can access it like you show:
or using dot notation:
JavaScript do not have 2D associative array as such. But 2d associative array can be realized through below code:
simply
var infArray = [{name: 'John'}, {name: 'Greg'}]
;-)