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
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 ->.
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]);
Related Topics
The Behaviour of the or Operator in PHP
Upgrading Laravel 5.5 to 5.6 Error
How to Find "Related Items" in PHP
Jquery:: Ajax Powered Progress Bar
Missing CSS File and Images After Url Rewrite
Does HTML_Entity_Decode Replaces &Nbsp; Also? If Not How to Replace It
Unchecked Checkbox Returning Null Value
How to Keep a PHP Session Active Even If the Browser Is Closed
Find in Set in Laravel? Example
What Is the Js Equivalent to the PHP Function Number_Format
Datetime Now PHP MySQL (+ Pdo Variant)
How to Skip Elements in Foreach Loop
PHP Dynamically Create CSV: Skip the First Line of a CSV File