PHP Error: Cannot use object of type stdClass as array (array and object issues)
The example you copied from is using data in the form of an array holding arrays, you are using data in the form of an array holding objects. Objects and arrays are not the same, and because of this they use different syntaxes for accessing data.
If you don't know the variable names, just do a var_dump($blog);
within the loop to see them.
The simplest method - access $blog as an object directly:
Try (assuming those variables are correct):
<?php
foreach ($blogs as $blog) {
$id = $blog->id;
$title = $blog->title;
$content = $blog->content;
?>
<h1> <?php echo $title; ?></h1>
<h1> <?php echo $content; ?> </h1>
<?php } ?>
The alternative method - access $blog as an array:
Alternatively, you may be able to turn $blog
into an array with get_object_vars
(documentation):
<?php
foreach($blogs as &$blog) {
$blog = get_object_vars($blog);
$id = $blog['id'];
$title = $blog['title'];
$content = $blog['content'];
?>
<h1> <?php echo $title; ?></h1>
<h1> <?php echo $content; ?> </h1>
<?php } ?>
It's worth mentioning that this isn't necessarily going to work with nested objects so its viability entirely depends on the structure of your $blog
object.
Better than either of the above - Inline PHP Syntax
Having said all that, if you want to use PHP in the most readable way, neither of the above are right. When using PHP intermixed with HTML, it's considered best practice by many to use PHP's alternative syntax, this would reduce your whole code from nine to four lines:
<?php foreach($blogs as $blog): ?>
<h1><?php echo $blog->title; ?></h1>
<p><?php echo $blog->content; ?></p>
<?php endforeach; ?>
Hope this helped.
Uncaught Error: Cannot use object of type stdClass as array
Use json_decode($src, true)
to get associative array.
This is preferred way, as currently you get mixed arrays and objects and you may end up in crazy house, trying to work with these :)
Alternatively use ->
operator to get properties of object.
Currently your item is at:
$code->items[0]->pagemap->cse_image[0]->src
Cannot use object of type stdClass as array in
function deliciousCount($domain_name) {
$data = json_decode(
file_get_contents(
"http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
)
);
// You should double check everything because this delicious function is broken
if (is_array($data) && isset($data[ 0 ]) &&
$data[ 0 ] instanceof stdClass && isset($data[ 0 ]->total_posts)) {
return $data[ 0 ]->total_posts;
} else {
return 0;
}
}
Cannot use object of type stdClass as array
To use result of query as array say
$result=$query->result_array();
because by default result is returned as object
Cannot use object of type stdClass as array Laravel Error
You are not assigning anything to the $cases
variable that you are returning to the view. So in this line:
$this->getCaseData($id);
You call the function below which sets $this->data
, but it doesn't assign$case
at any point in the openingPage()
method. When you then return the view with a variable called 'cases', it has nothing to send:
return view('caseopener', ['cases' => $cases]);
Either assign the variable $cases: $cases = $this->getCaseData($id);
or return the class variable:
return view('caseopener', ['cases' => $this->data]);
PDO error Cannot use object of type stdClass as array
Try $row_2->subject
instead of $row_2['subject']
When accessing StdClass objects use ->.
Fatal Error: Cannot use object of type stdClass as array?
They must have changed their API, as their response looks like this:
{
"1": {
"station": {
"id": 2,
"name": "test",
"shortcode": "test",
"description": "",
"frontend": "shoutcast2",
"backend": "liquidsoap",
"listen_url": "https:\\/\\/guardi.wearebounce.net:8010\\/radio.mp3?1572279272",
"is_public": true,
"mounts": [{
"path": "\\/radio.mp3",
"is_default": true,
"id": 4,
"name": "128kbps MP3",
"url": "https:\\/\\/guardi.wearebounce.net:8010\\/radio.mp3?1572279272",
"bitrate": 128,
"format": "mp3",
"listeners": {
"current": 0,
"unique": 0,
"total": 0
}
}],
"remotes": []
},
"listeners": {
"current": 0,
"unique": 0,
"total": 0
},
"live": {
"is_live": false,
"streamer_name": ""
},
"now_playing": {
"elapsed": 2085856,
"remaining": 0,
"sh_id": 16422,
"played_at": 1570193422,
"duration": 0,
"playlist": "",
"is_request": false,
"song": {
"id": "ab574fe34f592faf5c3f27dbc52088a4",
"text": "AzuraCast.com - AzuraCast is Live!",
"artist": "AzuraCast.com",
"title": "AzuraCast is Live!",
"album": "",
"lyrics": "",
"art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
"custom_fields": []
}
},
"playing_next": null,
"song_history": [{
"sh_id": 16421,
"played_at": 1570193242,
"duration": 0,
"playlist": "",
"is_request": false,
"song": {
"id": "a723e3dfb497b24c5f730bad44328916",
"text": "Animals",
"artist": "",
"title": "Animals",
"album": "",
"lyrics": "",
"art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
"custom_fields": []
}
}, {
"sh_id": 16419,
"played_at": 1570193227,
"duration": 0,
"playlist": "",
"is_request": false,
"song": {
"id": "de05db4a94a8e9b547504e9e51682bfd",
"text": "Bounce_ - _Bed_6",
"artist": "Bounce_",
"title": "_Bed_6",
"album": "",
"lyrics": "",
"art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
"custom_fields": []
}
}, {
"sh_id": 16417,
"played_at": 1570193032,
"duration": 0,
"playlist": "",
"is_request": false,
"song": {
"id": "ee0707f2222751ce78d5f2f358c50312",
"text": "Gangaman",
"artist": "",
"title": "Gangaman",
"album": "",
"lyrics": "",
"art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
"custom_fields": []
}
}, {
"sh_id": 16415,
"played_at": 1570192881,
"duration": 0,
"playlist": "",
"is_request": false,
"song": {
"id": "ad3cbc1061248ee1f594ca5932b35bbb",
"text": "Brave - Don Diablo",
"artist": "Brave",
"title": "Don Diablo",
"album": "",
"lyrics": "",
"art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
"custom_fields": []
}
}, {
"sh_id": 16414,
"played_at": 1570192852,
"duration": 0,
"playlist": "",
"is_request": false,
"song": {
"id": "ab574fe34f592faf5c3f27dbc52088a4",
"text": "AzuraCast.com - AzuraCast is Live!",
"artist": "AzuraCast.com",
"title": "AzuraCast is Live!",
"album": "",
"lyrics": "",
"art": "https:\\/\\/wearebounce.net\\/branding\\/LogoIconColour.png",
"custom_fields": []
}
}],
"cache": "station"
}
}
So they have an object with a key of 1, instead an array of objects. Since it's an object, you'll have to access it via the key. And since the key is a number, you need to get it with
$response = $api->{1};
Unfortunately, chaining doesn't work well (at least for me), so you'll have to assign it to a variable as above. Then you can use
$response->live->is_live;
Related Topics
Does PHP Allow Named Parameters So That Optional Arguments Can Be Omitted from Function Calls
Ajax and PHP to Enter Multiple Forms Input to Database
Sending Email Via PHP Mail Function Goes to Spam
How to Minify PHP Page HTML Output
PHP Echo VS PHP Short Echo Tags
Create Word Document Using PHP in Linux
PHP Error: Fatal Error: Constant Expression Contains Invalid Operations
Laravel: Syntax Error or Access Violation: 1055 Error
List of All Locales and Their Short Codes
Running MySQL *.SQL Files in PHP
Convert Flat Array to a Delimited String to Be Saved in the Database
How to Run a .Bat File from PHP
PHP Reading Shell_Exec Live Output
Preserving Session Variables Across Different Domains
How to Increase the Execution Timeout in PHP
How to Get Input Field Value Using PHP
How to Retract a Salted Password from the Database and Auth User