Adamhumbug Posted September 11, 2023 Share Posted September 11, 2023 I have an ajax function function getConsumablePrices($itemId, $qty) { $.ajax({ type: 'post', data: { "ajax": 'getConsumablePriceByQty', 'itemId': $itemId, 'quantity': $qty }, dataType: 'json', success: function(resp) { console.log(resp) } }) } This triggers a php function which after doing a fetch all returns an array that looks like this { "id": 1, "item_id": 19, "min_qty": 1, "max_qty": 499, "GBP": 500, "USD": 750, "CAD": 875 } I am trying to access individual elements in this object. I have tried console.log(resp['CAD'] and resp.CAD but i just see undefined. How would one go about pulling a single element out of this array? Quote Link to comment https://forums.phpfreaks.com/topic/317277-access-single-array-element/ Share on other sites More sharing options...
Barand Posted September 11, 2023 Share Posted September 11, 2023 console.log(resp.CAD) should work - if there is a resp. Check developer tools network i browser. Quote Link to comment https://forums.phpfreaks.com/topic/317277-access-single-array-element/#findComment-1611753 Share on other sites More sharing options...
Adamhumbug Posted September 12, 2023 Author Share Posted September 12, 2023 14 hours ago, Barand said: console.log(resp.CAD) should work - if there is a resp. Check developer tools network i browser. console.log(resp[0]['CAD']) - gives me the data i was expecting console.log(resp.CAD) - gives me undefined Quote Link to comment https://forums.phpfreaks.com/topic/317277-access-single-array-element/#findComment-1611764 Share on other sites More sharing options...
Barand Posted September 12, 2023 Share Posted September 12, 2023 6 minutes ago, Adamhumbug said: console.log(resp[0]['CAD']) - gives me the data i was expecting In that case, the sample json data you posted it not the same format as the data you actually received Quote Link to comment https://forums.phpfreaks.com/topic/317277-access-single-array-element/#findComment-1611765 Share on other sites More sharing options...
Adamhumbug Posted September 12, 2023 Author Share Posted September 12, 2023 5 minutes ago, Barand said: In that case, the sample json data you posted it not the same format as the data you actually received The data that i pasted came from the inspector window as the response. Quote Link to comment https://forums.phpfreaks.com/topic/317277-access-single-array-element/#findComment-1611766 Share on other sites More sharing options...
Solution Barand Posted September 12, 2023 Solution Share Posted September 12, 2023 For the "[0]" to be needed then the json data would need to be [{ "id": 1, "item_id": 19, "min_qty": 1, "max_qty": 499, "GBP": 500, "USD": 750, "CAD": 875 }] with the outer [..] to make it an array. Quote Link to comment https://forums.phpfreaks.com/topic/317277-access-single-array-element/#findComment-1611767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.