jarvis Posted January 6, 2017 Share Posted January 6, 2017 Hi, Apologies If I've got the title wrong but I'm trying to obtain a value from an array, however, it's an object (?) Array ( [test] => WC_Coupon Object ( [code] => test [id] => 1529 [exists] => 1 [discount_type] => percent_product [coupon_amount] => 10 [individual_use] => yes [product_ids] => Array I need to get coupon_amount I thought I could use $my_coupon[0]->coupon_amount; but believe I've confused myself or misunderstood! Any advice is gratefully received! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/302881-get-object-variable/ Share on other sites More sharing options...
Solution requinix Posted January 6, 2017 Solution Share Posted January 6, 2017 That output is for $my_coupon, right? $my_coupon Array (is an array [test]with a "test" key => WC_Coupon Objectwith a WC_Coupon object as the value [coupon_amount]which has a "coupon_amount" property => 10with the value 10. So $my_coupon["test"]->coupon_amountDoing it this way means you need to know the coupon code. Do you? Quote Link to comment https://forums.phpfreaks.com/topic/302881-get-object-variable/#findComment-1541099 Share on other sites More sharing options...
jarvis Posted January 6, 2017 Author Share Posted January 6, 2017 Thanks @requinix When I try I get array For testing, I do know the coupon name but won't going forward (that's my next task) Quote Link to comment https://forums.phpfreaks.com/topic/302881-get-object-variable/#findComment-1541100 Share on other sites More sharing options...
jarvis Posted January 6, 2017 Author Share Posted January 6, 2017 Apologies @requinix that did work - I was being a numpty! Quote Link to comment https://forums.phpfreaks.com/topic/302881-get-object-variable/#findComment-1541101 Share on other sites More sharing options...
requinix Posted January 6, 2017 Share Posted January 6, 2017 If you don't know the key and there's only one entry in the array then you can use current to get the value: current($my_coupon)->coupon_amount Quote Link to comment https://forums.phpfreaks.com/topic/302881-get-object-variable/#findComment-1541117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.