Fatal error: Cannot use object of type stdClass as array in
CodeIgniter returns result rows as objects, not arrays. From the user guide:
result()
This function returns the query result as an array of objects, or an empty array on failure.
You'll have to access the fields using the following notation:
foreach ($getvidids->result() as $row) {
$vidid = $row->videoid;
}
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
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;
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
PDO error Cannot use object of type stdClass as array
Try $row_2->subject
instead of $row_2['subject']
When accessing StdClass objects use ->.
Related Topics
How to Display Woocommerce Category Image
Why Are Certain Types of Prepared Queries Using Pdo in PHP with MySQL Slow
How to Get a User's Instagram Feed
Easy Login Script Without Database
Setting Up a Ubuntu/Apache/PHP MAChine to Send Email
Instagram Login Programmatically
How to Get List of Defined Namespaces
How to Get All Keys of an Multi Level Associative Arrays in PHP
How to Post Soap Request from PHP
Ajax/PHP - How to Get Posted Data to Load into a Modal on the Same Page
How to Trigger Xdebug Profiler for a Command Line PHP Script