从php中的print_r访问变量

时间:2016-05-16 06:04:03

标签: php

我得到了一个print_r($ result)输出,如下所示:

stdClass Object ( 
    [balance] => 998 
    [batch_id] => 243941208 
    [cost] => 1 
    [num_messages] => 1 
    [message] => stdClass Object ( 
        [num_parts] => 1 
        [sender] => TMTLCO 
        [content] => @U0D070D240D4D00200D120D300D4100200D1F0D460D380D4D0D310D4D0D310D4D002000200D060D230D4D 
    ) 
    [receipt_url] => 
    [custom] => 
    [messages] => Array ( 
        [0] => stdClass Object ( 
            [id] => 117250619 
            [recipient] => XXXXXXXX 
        ) 
    ) 
    [status] => success 
)

我可以回应以下内容并获得正确的输出:

echo $result->balance;
echo $result->num_messages;

但以下是行不通的?

echo $result->message->sender;
echo $result->messages->recipient;
echo $result->messages->status;

我做错了什么?

1 个答案:

答案 0 :(得分:4)

错过0这是一个数组。要访问recipient,您需要在$result数组中添加一个维度。

消息还有一个您错过导航的阵列。所以试试这个,

echo $result->message->sender;
echo $result->messages[0]->recipient;
echo $result->status;