0

Apologies noob question and I'm usually good at figuring this out.

I have an array with the values:

{
    "carriage": [{"Net":"586.54999999999995"}],
    "totalNet":[{"Net":"16734657.34"}],
    "creditCarrage":[{"Net":"9.5"}],
    "creditTotal":[{"Net":"5712.6999999999998"}]
}

I've tried to return to screen just the carriage net value however I get the error

Undefined index: Net

From the attempt

return $stats['carriage']['Net'];
0

1 Answer 1

3

Look carefully the 'carriage' is a 2 level depth array, the 'Net' is accessible like this:

return $stats['carriage'][0]['Net'];

Probably, you want to restructure the data.

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

3 Comments

I tried that but got the error: Cannot use object of type stdClass as array
it seems that {"Net":"586.54999999999995"} is an object. I could not tell as objects and associative arrays are represented the same in JSON. However, you can access it like this :return $stats['carriage'][0]->Net;
I would second that and also say that you probably want to restructure your data. If you don't need an array of objects then you could convert it to a single object and return your output like $stats->carriage->Netor $stats['carriage']->Net

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.