php - Accessing values of JSON object -


hy all, have problems accessing values json object.

first json_encode($something) "something" result query db. (precisely 3 rows). in .js did following: var data = ?php echo $something; ?>;

i tested in firebug , data is(i show 1 row):

[object {x="something1", y="something2", z="10", w="20"}, ...] array of objects. 

now need access these values not know names nor count, can't use console.log(data[0].x) work fine , output "something1";

when try little more generic way like:

for(var i=0; i<data.length; i++) {     for(var prop in data[i]) {         if(data[i].hasownproperty(prop))         console.log(data[i].prop);                       } } 

i "undefined" each value. how solve this. thanks.

i think problem here because of line

console.log(data[i].prop);

beacause of dot notation, expects object have prop key , not value of prop

try instead (use bracket notation instead)

console.log(data[i][prop]); // make sure first fetch value  //of propertyname , search key on 

square bracket notation allows access properties containing special characters , selection of properties using variables

check fiddle


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -