我如何阅读此对象的内容

问题描述 投票:0回答:2

我正在用PHP开发一个SMS阅读应用程序。我将SMS门户连接到应用程序并接收SMS。

请帮我阅读这个数组对象并分别获取内容。

array(3) 
{ 
    [0]=> object(stdClass)#14 (8) 
        { 
            ["message"]=> string(3) "G56" 
            ["messageId"]=> int(1) 
            ["recipients"]=> string(11) "94714369777" 
            ["retries"]=> int(1) 
            ["sender"]=> object(stdClass)#15 (1) 
                { 
                    ["alias"]=> string(11) "94710200542" 
                } 
            ["sequenceNum"]=> int(1) 
            ["status"]=> int(1) 
            ["time"]=> string(25) "2018-10-13T10:40:17+05:30" 
        } 
    [1]=> object(stdClass)#16 (8) 
        { 
            ["message"]=> string(4) "A67i" 
            ["messageId"]=> int(1) 
            ["recipients"]=> string(11) "94714369777" 
            ["retries"]=> int(1) 
            ["sender"]=> object(stdClass)#17 (1) 
                { 
                    ["alias"]=> string(11) "94710200542" 
                } 
            ["sequenceNum"]=> int(1) 
            ["status"]=> int(1) 
            ["time"]=> string(25) "2018-10-13T10:40:21+05:30" 
        } 
    [2]=> object(stdClass)#18 (8) 
        { 
            ["message"]=> string(6) "Vhhj99" 
            ["messageId"]=> int(1) 
            ["recipients"]=> string(11) "94714369777" 
            ["retries"]=> int(1) 
            ["sender"]=> object(stdClass)#19 (1) 
                { 
                    ["alias"]=> string(11) "94710200542" 
                } 
            ["sequenceNum"]=> int(1) 
            ["status"]=> int(1) 
            ["time"]=> string(25) "2018-10-13T10:40:24+05:30" 
        } 
} 
php web
2个回答
0
投票

它是一个stdObject,因此你可以像一个简单的对象一样访问它:

foreach($messages as $sms) {

   $message = $sms->message;
   $messageId = $sms-> messageId;
   $recipients = $sms-> recipients;
   $retries = $sms->retries;
   $sender = $sms->sender['alias'];
   $sequenceNum = $sms->sequenceNum;
   $status = $sms->status;
   $time = $sms->time;

   //...Do your work here
}

如果需要,可以将其转换为数组并访问数组字段等属性

$arr = (array) $sms;
$arr['message'];

0
投票

使用 - >运算符读取对象值

foreach($array as $key => $value){
  $message = $value->message;
}
© www.soinside.com 2019 - 2024. All rights reserved.