Reputation: 625
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
Upvotes: 0
Views: 74
Reputation: 3055
in php you can get the keys with array_keys($array)
, or just iterate the array like so:
foreach ($array as $key => $value) {
// ...
}
Upvotes: 1