imgrooot Posted March 27, 2017 Share Posted March 27, 2017 "status": "success", "data": { "network": "DOGE", "prices": [ { "price": "0.00003556", "price_base": "LTC", "exchange": "cryptsy", "time": 1401185325 } ] } I know I can access the top value like this. echo $access->status; But how do I access the sub values underneath? The ones in "data"? Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/ Share on other sites More sharing options...
Jacques1 Posted March 27, 2017 Share Posted March 27, 2017 How about $access->data->... Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544619 Share on other sites More sharing options...
imgrooot Posted March 28, 2017 Author Share Posted March 28, 2017 How about $access->data->... Yes I can get the sub values like this $access->data->network. But how do I get the sub values of prices? Like this for eg. $access->data->prices->price_base. This gives me an error like this. Notice: Trying to get property of non-object in ../index.php on line 67 Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544701 Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 Maybe the square brackets are messing things up? Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544702 Share on other sites More sharing options...
imgrooot Posted March 28, 2017 Author Share Posted March 28, 2017 Maybe the square brackets are messing things up? Perhaps. So that's why i am asking how can I access the values within those square brackets? I have no control over that output data. I just need to access it as a variable. Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544703 Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 So you already KNEW that there was a problem and didn't mention it? Are you trying to say you have no control over the INPUT data? Output would be what you produce from this data. Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544704 Share on other sites More sharing options...
imgrooot Posted March 28, 2017 Author Share Posted March 28, 2017 So you already KNEW that there was a problem and didn't mention it? Are you trying to say you have no control over the INPUT data? Output would be what you produce from this data. I don't know if that's the issue or not. I am using this api. https://block.io/api/simple/php This line here. $block_io->get_current_price(array('price_base' => 'AUD')); will output this result. { "status" : "success", "data" : { "network" : "BTC", "prices" : [ { "price" : "1440.0", "price_base" : "AUD", "exchange" : "coinspot", "time" : 1490731591 }, { I would like to access the price, price_base, exchange and time data. I would like to convert them into a variable so I can echo them out. How can I do that? Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544706 Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 (edited) Since the data (from what you show us) appears to be a complex string, why not treat it that way and do a simple string replace of the brackets for braces and then use that 'string' in your script. Edited March 28, 2017 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544707 Share on other sites More sharing options...
imgrooot Posted March 28, 2017 Author Share Posted March 28, 2017 Since the data (from what you show us) appears to be a complex string, why not treat it that way and do a simple string replace of the brackets for braces and then use that 'string' in your script. Can you please show me how that string replace would look with the above code? Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544708 Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 Do some testing first. I'm not a json expert(waiting for Jacques to pipe in here anytime!) but why don't you create a test script to assign this 'input' to a json_decode function call and then var_dump it? Be sure error checking is turned on and do an experiment for us. Tell us what happens. I have tried it here but the data is really screwed up (or not fully posted) so it doesn't work for me. Are you sure these people are aware of the problems with this input? Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544710 Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 Followup. With a little massaging of the input the string can be imported to php as a string, an object or an array. The last looks like just what you want, but I had to remove a bracket and correct an extra brace in your posted sample, so there is no telling how many other errors you may be receiving from this (flawed) api. Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544711 Share on other sites More sharing options...
imgrooot Posted March 28, 2017 Author Share Posted March 28, 2017 Followup. With a little massaging of the input the string can be imported to php as a string, an object or an array. The last looks like just what you want, but I had to remove a bracket and correct an extra brace in your posted sample, so there is no telling how many other errors you may be receiving from this (flawed) api. Is it possible for you to paste that code you just worked on? It'll help me see. It is unfortunate the api. I emailed them many times and they never answer back. Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544718 Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 The massaging of the data was a manual effort. Importing the data as a string into php code - uh... If you can't do that then you are way outside your comfort zone. That said - I'll let you sit back and ponder the future. But the first thing you have to do is massage the data if they won't fix it. I don't know if there is some kind of regular expression that can do that for you or not. I am not at all conversant in those things. Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544719 Share on other sites More sharing options...
Kosonome Posted March 28, 2017 Share Posted March 28, 2017 (edited) "status": "success", "data": { "network": "DOGE", "prices": [ { "price": "0.00003556", "price_base": "LTC", "exchange": "cryptsy", "time": 1401185325 } ] } I know I can access the top value like this. echo $access->status; But how do I access the sub values underneath? The ones in "data"? Not sure if that's what you meant: [ ] = means array in JSON I think you need access like that: echo $access->data->prices[0]->price; or echo $access->data->prices[1]->price; // this will get an error, maybe the api can give you more than one "price" Edited March 28, 2017 by Kosonome Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544721 Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 (edited) Yes - we know. It's already been covered. Stop worrying about the access. That is the least of your concerns. You have to get the data readable first. Edited March 29, 2017 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544722 Share on other sites More sharing options...
ginerjm Posted March 29, 2017 Share Posted March 29, 2017 (edited) Just found something about brackets creating a list. But the sample data from the OP didn't fit that scheme. Now the quest is - did OP re-type the data and mess it up, or does he have a more-perfect sample to show us? Edited March 29, 2017 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544723 Share on other sites More sharing options...
Jacques1 Posted March 29, 2017 Share Posted March 29, 2017 The OP already has successfully decoded the data: Yes I can get the sub values like this $access->data->network. So the data is not incomplete, broken or whatever. The samples above are just shortened, or the OP doesn't know how to copy and paste. None of this matters. Kosonome got it right: This is a problem of not understanding arrays. $first_base_price = $access->data->prices[0]->base_price; Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544725 Share on other sites More sharing options...
ginerjm Posted March 29, 2017 Share Posted March 29, 2017 The data samples posted here were broke. But having Jacques here makes everything alright. Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544726 Share on other sites More sharing options...
Solution imgrooot Posted March 29, 2017 Author Solution Share Posted March 29, 2017 (edited) The OP already has successfully decoded the data: So the data is not incomplete, broken or whatever. The samples above are just shortened, or the OP doesn't know how to copy and paste. None of this matters. Kosonome got it right: This is a problem of not understanding arrays. $first_base_price = $access->data->prices[0]->base_price; That does the trick. It works perfectly now. Thank you. I am surprised they didn't show how to get this data in their API. Edited March 29, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544740 Share on other sites More sharing options...
Jacques1 Posted March 29, 2017 Share Posted March 29, 2017 (edited) Next time, try to ask specific questions. If you had told us right from the beginning which data you want, what you've tried and which errors you got, this would have been done in 5 minutes rather than two days. I am surprised they didn't show how to get this data in their API. Because a) this is standard JSON and b) they cannot possibly know how you're parsing their data. There are more languages than PHP. Edited March 29, 2017 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544741 Share on other sites More sharing options...
imgrooot Posted March 29, 2017 Author Share Posted March 29, 2017 (edited) Next time, try to ask specific questions. If you had told us right from the beginning which data you want, what you've tried and which errors you got, this would have been done in 5 minutes rather than two days. Because a) this is standard JSON and b) they cannot possibly know how you're parsing their data. There are more languages than PHP. Understood. I will do that from now on. Edited March 29, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/303549-how-do-i-access-these-values-using-php-variables/#findComment-1544744 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.