0

I have three data coming back from an ajax call. I pack them into an array like this:

return json_encode([$salesOrder, $soAddressDetails, $lineItems]);

I then go to the view and look at the return. I see (as an example) this:

[
    [{
        "id": 8591,
        "reference": "MYCLIENT",
        "name": "MYCLIENT COMPANY \u00a3",
        "allocated_status": "",
        "created_at": "2016-12-02 09:31:00",
        "order_date": "2016-12-02",
        "cust_order_number": "",
        "del_name": "",
        "consignment": "",
        "despatch_date": "0000-00-00",
        "notes_2": ""
    }],
    [],
    [{
            "id": 11691,
            "qty_ordered": 1,
            "qty_delivered": 0,
            "sales_order_id": 8591,
            "due_date": "2016-12-30",
            "stock_code": "ABC-ABDCDE-01",
            "record_deleted": 0,
            "updated_at": null,
            "unit_price": 0,
            "sales_order_item_id": null,
            "comment": null,
            "created_at": null,
            "firmware_version": null,
            "units_assigned": null
        },

        {
            "id": 11692,
            "qty_ordered": 1,
            "qty_delivered": 0,
            "sales_order_id": 8591,
            "due_date": "0000-00-00",
            "stock_code": "MISCELLANEOUS",
            "record_deleted": 0,
            "updated_at": null,
            "unit_price": 232,
            "sales_order_item_id": null,
            "comment": null,
            "created_at": null,
            "firmware_version": null,
            "units_assigned": null
        }
    ]
]

In theory all I should need to access this, as array result is:

result[0] // sales order details
result[2] // line items = array of objects

so

result[0].reference == 'MYCLIENT'

and

result[2][0].stockcode == 'ABC-ABDCDE-01

but it won't let me do that. if I console.log(result[0]) the result is [, if I console.log(result[0][0].id) the result is undefined.

what am I doing wrong?

5
  • 1
    Tested in the console and result[0][0].id worked. How are you trying to access it ? Where result comes from ? Commented Mar 14, 2017 at 17:19
  • 2
    Try doing JSON.parse(result), seems like your result is still a string Commented Mar 14, 2017 at 17:19
  • 1
    How are you retrieving the json from php? Ajax (jquery, angular, vanilla, etc..) or a regular request? Commented Mar 14, 2017 at 17:21
  • JSON PARSE! I forgot JSON.parse. It has been a long day. Suraj if you make that an answer I'll accept it. And thanks all. Commented Mar 14, 2017 at 17:27
  • Added! Yeah, happens to all of us! Commented Mar 14, 2017 at 17:29

3 Answers 3

2

From the last line of your question, seems like your result is still a string.

Try doing JSON.parse(result)

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

Comments

2

Either you need to say that your response will be Json in your ajax like this

 dataType: 'json'

Or after getting response you have to convert it into json object

response = JSON.parse(response);

Comments

0

I think you just miss the level. The reference it's also in a second level. So you need to access it like:

console.info(result[0][0].reference)

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.