imgrooot Posted March 8, 2017 Share Posted March 8, 2017 Say I have this array. $block_io->get_current_price(array()); That array will give me this output. { "status" : "success", "data" : { "network" : "BTC", "prices" : [ { "price" : "1500.01", "price_base" : "AUD", "exchange" : "coinspot", "time" : 1488955012 }, { How can I convert the above output into individual variables? For eg. $status = 'success'; $network = 'BTC'; $price = '1500.01'; ...etc Quote Link to comment https://forums.phpfreaks.com/topic/303393-how-do-i-convert-php-array-to-individual-variables/ Share on other sites More sharing options...
requinix Posted March 8, 2017 Share Posted March 8, 2017 That output is JSON. Are you doing a json_decode somewhere? That's the first step. It will return to you an array or object. Once you have that you don't need individual variables because you can do either $variable["status"] $variable->statusdepending whether you had it decode as an array or object. Quote Link to comment https://forums.phpfreaks.com/topic/303393-how-do-i-convert-php-array-to-individual-variables/#findComment-1543912 Share on other sites More sharing options...
imgrooot Posted March 8, 2017 Author Share Posted March 8, 2017 (edited) That output is JSON. Are you doing a json_decode somewhere? That's the first step. It will return to you an array or object. Once you have that you don't need individual variables because you can do either $variable["status"] $variable->statusdepending whether you had it decode as an array or object. My mistake. I am using this api and used their output because it was cleaner looking.https://block.io/api/simple/php But same as above, if I print_r the variable like this. $newInfo = $block_io->get_current_price(array()); print_r($newInfo); It will give me the output like this. object(stdClass)#5 (2) { ["status"]=> string(7) "success" ["data"]=> object(stdClass)#6 (2) { ["network"]=> string(3) "BTC" ["prices"]=> array(11) { [0]=> object(stdClass)#7 (4) { ["price"]=> string(6) "1590.0" ["price_base"]=> string(3) "AUD" ["exchange"]=> string( "coinspot" ["time"]=> int(1488981211) } [1]=> object(stdClass)#8 (4) { ["price"]=> string( "1201.203" ["price_base"]=> string(3) "USD" ["exchange"]=> string(5) "btc-e" ["time"]=> int(1488981211) } [2]=> object(stdClass)#9 (4) { ["price"]=> string(10) "1149.95741" ["price_base"]=> string(3) "EUR" ["exchange"]=> string(5) "btc-e" ["time"]=> int(1488981211) } [3]=> object(stdClass)#10 (4) { ["price"]=> string(7) "68920.0" ["price_base"]=> string(3) "RUR" ["exchange"]=> string(5) "btc-e" ["time"]=> int(1488981211) } [4]=> object(stdClass)#11 (4) { ["price"]=> string(7) "1203.14" ["price_base"]=> string(3) "USD" ["exchange"]=> string( "coinbase" ["time"]=> int(1488981213) } [5]=> object(stdClass)#12 (4) { ["price"]=> string(6) "1205.9" ["price_base"]=> string(3) "USD" ["exchange"]=> string( "bitfinex" ["time"]=> int(1488981214) } [6]=> object(stdClass)#13 (4) { ["price"]=> string(6) "1202.0" ["price_base"]=> string(3) "EUR" ["exchange"]=> string(7) "litebit" ["time"]=> int(1488981222) } [7]=> object(stdClass)#14 (4) { ["price"]=> string(7) "1206.61" ["price_base"]=> string(3) "USD" ["exchange"]=> string( "bitstamp" ["time"]=> int(1488981224) } [8]=> object(stdClass)#15 (4) { ["price"]=> string(6) "1154.2" ["price_base"]=> string(3) "EUR" ["exchange"]=> string( "bitstamp" ["time"]=> int(1488981224) } [9]=> object(stdClass)#16 (4) { ["price"]=> string(6) "8037.0" ["price_base"]=> string(3) "CNY" ["exchange"]=> string(4) "bter" ["time"]=> int(1488981225) } [10]=> object(stdClass)#17 (4) { ["price"]=> string(6) "7830.0" ["price_base"]=> string(3) "CNY" ["exchange"]=> string( "btcchina" ["time"]=> int(1488981208) } } } } Using your json_decode like this doesn't work. It doesn't return me anything. No error either. $variable = json_decode($newInfo); echo $variable->status; Edited March 8, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/303393-how-do-i-convert-php-array-to-individual-variables/#findComment-1543924 Share on other sites More sharing options...
requinix Posted March 8, 2017 Share Posted March 8, 2017 Then it's already been decoded into $newInfo. Try doing ->status on that. Quote Link to comment https://forums.phpfreaks.com/topic/303393-how-do-i-convert-php-array-to-individual-variables/#findComment-1543925 Share on other sites More sharing options...
Solution imgrooot Posted March 8, 2017 Author Solution Share Posted March 8, 2017 Then it's already been decoded into $newInfo. Try doing ->status on that. Oh wow, it works. That was simple. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/303393-how-do-i-convert-php-array-to-individual-variables/#findComment-1543927 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.