How to loop through a json array and get the data through variables,im using the poloniex api for getting prices via php here is the print_r used with pre tags
Array
(
[asks] => Array
(
[0] => Array
(
[0] => 0.02318590
[1] => 0.09
)
[1] => Array
(
[0] => 0.02318594
[1] => 3.93
)
....
for more of the output https://wiloop/wolftrader/trades.php
This should do it:
$array = [YOUR ARRAY]
foreach ($array["asks"] as $price){
print $price[0];
print $price[1];
}
UPDATE: just adding a bit more detail on how this works so others with similar issue get a bit more insight.
Basically, the array above is already a PHP array, so all we really have to do is to iterate that array - PHP knows how to do it and "foreach" is our best bet here. It takes an array and iterates through each of its components assigning it to a variable (which we called "price" in the example below).
The little nuance we have here is that the main array has a single element, called "asks", so that's the array we want to iterate. If we iterate the main one the only thing we'll get is this one element - "asks". If we iterate that element directly then we get access to the data that we need.
Within the "foreach" iterator, "price" will get assigned to that value and because price itself is an array with 2 elements, we then can access its values by using the PHP array syntax: $price[index], where index is the position of the element we want in the array.
Anyways, just sharing a bit more detail so hopefully it's helpful for anyone getting started with Arrays in PHP.