1

I have the following array of objects:

Array
(
    [0] => stdClass Object
        (
            [original_price] => 1100
            [discounted_price] => 1100
        )
)

I am trying to access original and discounted price using javascript. But not getting.
I have tried result[0]['original_price'] where result is the total result but not getting the value.

7
  • 3
    Are you sure this belongs under the javascript tag? That is a php array. Commented Aug 2, 2016 at 6:14
  • can you show the javascript object (not this pictogram ) Commented Aug 2, 2016 at 6:14
  • This array I am getting as a result of ajax call, and I have to use javascript compulsary to access the same Commented Aug 2, 2016 at 6:14
  • You need to use json_encode($array) in your PHP code Commented Aug 2, 2016 at 6:17
  • 1
    Send from server as json_encode($array) & then use JSON.parse(arr) on client side. Commented Aug 2, 2016 at 6:18

3 Answers 3

2

Why are you returning array itself from ajax call? Instead you have to return an JSON Object like:

json_encode($array); 

And then in your ajax response use it like:

var data = JSON.parse(response);

now use data accordingly

Sign up to request clarification or add additional context in comments.

Comments

0

In JS, you should use array_name.object_name format

Comments

0

If you are trying to get this value in javascript then you can try with below way:

https://jsfiddle.net/ofvcahhr/

var arr = {};

arr[0] = {
          "origanl_price": 1100,
          "discounted_price": 100
          };

alert(arr[0].origanl_price);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.