Page 1 of 1

Class returns object convert it to array...

Posted: Thu Jul 05, 2007 7:00 pm
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

Posted: Thu Jul 05, 2007 7:09 pm
by vigge89
echo $price->list[0]['rate']; ??

Posted: Thu Jul 05, 2007 8:11 pm
by tecktalkcm0391
vigge89 wrote:echo $price->list[0]['rate']; ??
Nope, didn't work.

Posted: Thu Jul 05, 2007 8:18 pm
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;

Posted: Thu Jul 05, 2007 8:30 pm
by tecktalkcm0391
thanks! it worked! :D

Posted: Thu Jul 05, 2007 8:38 pm
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! :)

Posted: Thu Jul 05, 2007 8:45 pm
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.

Posted: Fri Jul 06, 2007 7:50 am
by vigge89
Ah, I missed the fact that list[0] was an object - my bad ;)