0

This question has been answered several times yet I can't get it to fit my situation (down to stupidity more than lack of examples).

For whatever reason, the developer of this JSON API has put part of the data in the keys, therefore I need to access the keys and the corresponding data to get both bits of info I need.

NOTE - I'm placing the JSON in an array, not an object.

$json = json_decode($contents, true);    

Here's an example dump from my JSON feed:

Array
(
[code] => 1
[data] => Array
    (
        [sell] => Array
            (
                [0.00000060] => 7305
                [0.00000061] => 567068
                [0.00000065] => 43465
            )
    )

To access a part of it, I would currently have to use something similar to:

$var1 = $json['data']['sell']['0.00000060'];

How can I access the keys in a similar method? Ultimately I will be storing both bits of info in a variable and need to end up with something similar to:

[0]
Price: 0.00000060
Quantity: 7305

[1]
Price: 0.00000061
Quantity: 567068

[2]
Price: 0.00000065
Quantity: 43465

Hope this makes sense,

Thanks,

EDIT: For anyone else who is stuck, this is how I done it using the example above:

 $array = $json['data']['sell'];

    foreach ($array as $key => $value) {
         echo $key . "<br/>";
    }

Returns:

0.00000060
0.00000061
0.00000065

1 Answer 1

1

in php you can get the keys with array_keys($array), or just iterate the array like so:

foreach ($array as $key => $value) {
    // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Excuse my ignorance, but could you elaborate on this and show me how you would echo out all the keys seen in my example? I really can't get it to work :( Much appreciated
Nevermind - I finally got it to work - Thank you very much for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.