简体   繁体   English

PHP从数组内的数组中获取价值

[英]PHP Get value from array within array

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: 然后,例如,如果我想检索message-id,我正在尝试:

$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! 预先感谢您的任何帮助!

message-count appears to be 1 . message-count似乎是1 messages is an array of arrays, and that array contains message-id . messages是一个数组数组,该数组包含message-id To get the first message id, you would use 要获取第一个消息ID,您可以使用

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

Note how I formatted your code snippet above. 请注意我如何格式化上面的代码片段。 You forgot the [0] portion, so your assignment would be 您忘记了[0]部分,因此您的作业将是

$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. 不需要['message-count'] ,因为它不是您要关闭的“ tree”分支的一部分。

你只是想念0

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM