0

I'm sorry if this is a basic question but I'm just having trouble getting a value from an array within an array. Here is my code:

$data = Array (
    [message-count] => 1
    [messages] => Array (
         [0] => Array (
               [to] => 19998887777
               [message-price] => 0.00550000
               [status] => 0
               [message-id] => 0300000021B6B103
               [remaining-balance] => 18.59500000
               [network] => 31000
          )
     )
);

Then, for example, if I wanted to retrieve the message-id, I'm trying:

$messageID = $data['message-count']['messages']['message-id'];

That doesn't return anything so I know I'm missing something simple. Thank you in advance for any help!

3 Answers 3

3

message-count appears to be 1. messages is an array of arrays, and that array contains message-id. To get the first message id, you would use

$data["messages"][0]["message-id"]
Sign up to request clarification or add additional context in comments.

Comments

2

Note how I formatted your code snippet above. You forgot the [0] portion, so your assignment would be

$messageID = $data['messages'][0]['message-id'];
                              ^^^---forgot this

The ['message-count'] is not necessary, because it's not part of the "tree" branch you're going down.

Comments

0

You just miss the 0

$data["messages"][0]["message-id"]

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.