简体   繁体   English

获取:“尝试获取非对象的属性”时尝试访问对象数组中的值

[英]Getting: ' Trying to get property of non-object' when trying to access the value in an array of objects

So from a Get request I get back a response in this structure:因此,从 Get 请求中,我得到了这个结构的响应:

{
  "chromeosdevices" : [
     {
         "somekey" : "somevalue",
         "annotatedUser" : "annotatedUserValue",
         "activeTimeRanges" : [
             {
                 "date" : "dateValue",
                 "activeTime" : "activeTimeValue"
             }
          ],
       "somekey" : "somevalue",
      },
   ]
}

How can I access both DateValue and ActiveTimeValue?如何同时访问 DateValue 和 ActiveTimeValue?

I have tried it with this code:我已经用这段代码试过了:

$dec = json_decode($response);
$filteredResults= array();
if (! empty($dec->chromeosdevices)) {
    foreach ($dec->chromeosdevices as $chromeosdevice) {
        $user['annotatedUser'] = $chromeosdevice->annotatedUser;
        $user['activeTimeRanges_date'] = $chromeosdevice->activeTimeRanges->date;
        $user['activeTimeRanges_activeTime'] = $chromeosdevice->activeTimeRanges -> activeTime;
        $filteredResults[] = $user;
    }
}
echo '<pre>';print_r($filteredResults);echo '</pre>';

However, I only get back the annotatedUser, but not the date and active time.但是,我只取回了 annotatedUser,而不是日期和活动时间。 Why is that?这是为什么?

As it is another object in an array use $chromeosdevice->activeTimeRanges[0]->date;因为它是数组中的另一个 object,所以使用$chromeosdevice->activeTimeRanges[0]->date;

That is in an array so maybe another loop would be the safest way to deal with this那是在一个数组中,所以也许另一个循环是处理这个问题的最安全方法

$dec = json_decode($response);
$filteredResults= array();
if (! empty($dec->chromeosdevices)) {
    foreach ($dec->chromeosdevices as $chromeosdevice) {
        $user = [];     // init and remove last loops info
        $user['annotatedUser'] = $chromeosdevice->annotatedUser;
        foreach ($chromeosdevice->activeTimeRanges as $tr) {
            $user['activeTimeRanges_date'][]       = $tr->date;
            $user['activeTimeRanges_activeTime'][] = $tr->activeTime;
        }
        $filteredResults[] = $user;
    }
}
echo '<pre>';print_r($filteredResults);echo '</pre>';

暂无
暂无

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

相关问题 尝试访问数组时尝试获取非对象的属性 - Trying to get property of non-object when trying to access array 对象数组:想访问一个属性,但是我得到“试图在…中获取非对象的属性” - array of objects: want to access one property but I get “Trying to get property of non-object in …” 注意:尝试使用对象数组获取非对象的属性 - Notice: Trying to get property of non-object With Array of objects 使用时获取[尝试获取非对象的属性] -&gt; - Getting [Trying to get property of non-object] when using -> 在变量中获取值时尝试获取非对象的属性 - Trying to get property of non-object when get value in variable 为什么在尝试访问“$comment-&gt;users-&gt;username”时出现“试图获取非对象的属性‘用户名’”? - Why am I getting "Trying to get property 'username' of non-object" when trying to access "$comment->users->username"? 试图在视图返回中访问数组试图获取非对象的属性 - Trying to access to array in view return Trying to get property of non-object 获取消息错误PHP:尝试在Codeigniter中打印数据时尝试获取非对象的属性 - Getting message error PHP: Trying to get property of non-object when trying to print my data in Codeigniter 尝试在视图上访问Auth :: user() - > name时尝试获取非对象[Laravel]的属性 - Trying to get property of non-object [Laravel] when trying to access Auth::user()->name on view PHP-错误:尝试为购物车数组元素分配变量时,“试图获取非对象的属性” - PHP - error: 'Trying to get property of non-object' when trying to assign variables with shopping cart array elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM