Jump to content

How do I access these values using php variables?


imgrooot
Go to solution Solved by imgrooot,

Recommended Posts

"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"?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

"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 by Kosonome
Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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;
Link to comment
Share on other sites

  • Solution

 

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 by imgrooot
Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

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 by imgrooot
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.