简体   繁体   English

如何在php中访问stdclass对象?

[英]how to access stdclass object in php?

This is my object: 这是我的目标:

$reportdata = stdClass Object
(
    [1] => stdClass Object
        (
            [reportname] => reportname1
            [reportviews] => 20
            [reportpath] => reports/reportname1
            [thumbnailurl] => reports/thumbnailurl/thumbnailurl1.jpg
            [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        )

    [2] => stdClass Object
        (
            [reportname] => reportname2
            [reportviews] => 20
            [reportpath] => reports/reportname2
            [thumbnailurl] => reports/thumbnailurl/thumbnailurl2.jpg
            [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        )


)

I am trying to access the individual values using $reportdata[1]['reportviews'] but I get an error 我试图使用$reportdata[1]['reportviews']访问各个值但我收到错误

"Cannot use object of type stdClass as array". “不能使用stdClass类型的对象作为数组”。

How can I access reportname and reportviews ? 如何访问reportnamereportviews

像这样: $reportdata->{1}->reportviews

Try like this 试试这样吧

 $reportdata=json_decode(json_encode($reportdata),true);

then use this 然后用这个

$reportdata[1]['reportviews']

You will not get any error. 你不会得到任何错误。

$reportdata->{'1'}->reportname;
$reportdata->{'1'}->reportviews;

You can access the properties of the object like others have mentioned but wanted to give an alternative option. 您可以像其他人提到的那样访问对象的属性,但是想要提供替代选项。

get_object_vars — Gets the properties of the given object get_object_vars - 获取给定对象的属性

array get_object_vars ( object $object ) array get_object_vars(object $ object)

http://php.net/manual/en/function.get-object-vars.php http://php.net/manual/en/function.get-object-vars.php

$arrReportData = get_object_vars($reportdata);
echo $arrReportData[1]->reportviews;

Note that this is non-recursive, so in your case you'll get an array of objects. 请注意,这是非递归的,因此在您的情况下,您将获得一个对象数组。

The way you trying to access an element: 您尝试访问元素的方式:

$reportdata[1]['reportviews']

suggest that the main object is an associate array. 建议主要对象是关联数组。 This code would work if your object is declare like below: 如果您的对象声明如下,此代码将起作用:

$reportdata = array(
  '1' => array (
    'reportname' => 'reportname1',
    'reportviews' => '20',
    'reportpath' => 'reports/reportname1',
    'thumbnailurl' => 'reports/thumbnailurl/thumbnailurl1.jpg',
    'description' => 'Lorem ipsum dolor.'
  ),
  :
);

But when the stdClass Object is used, you can access an object property named in a variable as below: 但是当使用stdClass Object时,您可以访问变量中指定的对象属性,如下所示:

$atr1 = '1'; $atr2 = 'reportviews';
print $reportdata->$atr1->$atr2;

Or using an expression notation: 或者使用表达式表示法:

print $reportdata{'1'}{'reportviews'}; 

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

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