Gary Carlyle Cook
Gary Carlyle Cook

Reputation: 749

Print_r returns what I want but echo does not

I am trying to get the first "url" from what I think is an array.

This get the "url" works fine but it not usable obviously

print_r ($wantedimage[0]);

But this gets error:

Catchable fatal error: Object of class stdClass could not be converted to string.

When I try

echo $wantedimage[0];

What am I doing wrong please?

Upvotes: 0

Views: 1540

Answers (3)

iFarbod
iFarbod

Reputation: 639

Use var_dump or Kint. And also $wantedimage[0], what's the type of this? Integer, Float, String, Array, ..., What?

Upvotes: 2

Gary Carlyle Cook
Gary Carlyle Cook

Reputation: 749

Worked it out with help from the answers here and my own tenacity.

This worked for some reason. So please do not delete question as I flagged for if it is useful.

$wantedimage =$data-> images;

$a0=$wantedimage[0]->url;

echo $a0;

Thanks for the help.

Sorry I was so stressed about it. It's part of a bigger code with many nested levels.

Upvotes: 0

Matheno
Matheno

Reputation: 4152

You should use something like this:

foreach($wantedimage as $key => $value)
{
  echo $value;
}

Or based on it's structure:

$wantedimage[0]->url 

Upvotes: 4

Related Questions