Help understanding object

I am working with an api. I have code in place to get account results which do work. Here is the code


$Envato = new Envato_marketplaces();
$Envato->set_api_key('my api key');
$account_info = $Envato->account_information('my account name');

$Envato->prettyPrint($account_info);

the above code spits out this


stdClass Object
(
    [image] => path to my avatar
    [firstname] => my first name
    [surname] => my last name
    [total_earnings] => 0.00
    [total_deposits] => 0.00
    [balance] => 0.00
    [country] =>
    [current_commission_rate] => 0.0
)

Now I have been able to echo out the data above by doing a foreach loop into an un ordered list. It was when I tried to call it separately that I ran into trouble. I figured out that I can do this

echo '<img src="'. $account_info->image . '" />';

what this does is create the problem of what is the best way to find the fields that can be used under account_info. Is it common practice to print_r the object in the above array format to find the fields or is there a better way?

My main question that I would like answered if you please is how do I get the object into an array so that it can be accessed like so

echo $account_info[image]; 

You should know that my coding skills are beginner at best. Please try to keep that in mind if you offer up a solution. Thank you for your time.

print_r() is pretty much the standard way of finding what keys and values exist within an array as its the quickest code you can write without making some nice and pretty, as for converting the object to an array see the following link which explains how to do it as PHP doesn’t come with any built in functions that allows us to change the type cast on the fly.

PS: Changing the type cast can work in some circumstances but i would NOT recommend it as it can result in invalid results.

$account_info = (array) $account_info;

I cannot see why you’d prefer to use the array notation over object notation …

$account_info->image

… that expands in double quotes, is less typing and therefore less prone to errors than this:

$account_info[‘image’]

Also, when you come across variables using the object notation further down your script you will do a bit less head-scratching when you wonder to yourself “now where did this variable come from?” (oh, its from an array - no wait a sec, its actually from an object which I turned into an array … )

Perhaps there is another reason why you prefer array notation?

Probably because he’s not familiar with objects.

Take a look at get_object_vars() - that should give you what I think you’re looking for.