Class returns object convert it to array...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Class returns object convert it to array...

Post by tecktalkcm0391 »

My USPS Class returns the following from a print_r($price);

Code: Select all

usps Object
(
    [server] => http://production.shippingapis.com/ShippingAPI.dll
    [user] => 000XXXXX9999
    [pass] => 
    [service] => PRIORITY
    [dest_zip] => 20008
    [orig_zip] => 32186
    [pounds] => 10
    [ounces] => 5
    [container] => 
    [size] => Regular
    [machinable] => true
    [country] => USA
    [zone] => 5
    [list] => Array
        (
            [0] => price Object
                (
                    [mailservice] => Priority Mail
                    [rate] => 17.95
                )

        )

)
How can I get the rate? I tried echo($price['list']['0']['rate']); but nothing returned
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

echo $price->list[0]['rate']; ??
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

vigge89 wrote:echo $price->list[0]['rate']; ??
Nope, didn't work.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

-> accesses a property of an object
[] accesses an element of an array

From that you should be able to figure it out:

Code: Select all

echo $price->list[0]->rate;
(#10850)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

thanks! it worked! :D
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Kids these days! You can get them to access a property in an object, and even access an element in an array property -- but try to get them to access a property of an object in an array of objects that is the property of an object! :)
(#10850)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

arborint wrote:Kids these days! You can get them to access a property in an object, and even access an element in an array property -- but try to get them to access a property of an object in an array of objects that is the property of an object! :)
haha. thanks.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Ah, I missed the fact that list[0] was an object - my bad ;)
Post Reply