Jump to content
imgrooot

How do I access these values using php variables?

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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 by ginerjm

Share this post


Link to post
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

Share this post


Link to post
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;

Share this post


Link to post
Share on other sites

The data samples posted here were broke. But having Jacques here makes everything alright.

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.