2

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

"Cannot use object of type stdClass as array".

How can I access reportname and reportviews?

3
  • Maybe unrelated, but that's usually the mess you get when you try to convert an array without contiguous index into JSON and then back. Make sure you clean up your array index with array_values() before converting it to JSON to make sure it will be unserialized as a proper array instead of an object. Commented Jul 14, 2017 at 18:37
  • You can also use json_decode($json, true) so it creates everything as associative arrays instead of objects. Commented Jul 14, 2017 at 18:39
  • @Francis Please accept any answer below by clicking a green check mark. Commented Jul 19, 2017 at 14:04

5 Answers 5

7

Like this: $reportdata->{1}->reportviews

Sign up to request clarification or add additional context in comments.

1 Comment

The key thing here is that { } allows you to specify an expression as a property name.
1

Try like this

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

then use this

$reportdata[1]['reportviews'] 

You will not get any error.

7 Comments

json_decode expects a string and you're passing it an object. So... no.
Try it once again
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ). Setting assoc to true will only affect the returned result. Input is always expected to be a string.
If you didn't notice - there's json_encode($reportdata) which gives a string. So all is fine here.
i am just try to make the object to Array then whats wrong with that ?
|
0
$reportdata->{'1'}->reportname; $reportdata->{'1'}->reportviews; 

Comments

0

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

array get_object_vars ( object $object )

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.

Comments

0

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:

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

Or using an expression notation:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.