0

Why i can't get out the values from the json file with PHP? I get zero values? Have tried for hours.

    <?php


$json_string = 'http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=44';

$jsondata = file_get_contents($json_string);

$data = json_decode($jsondata, TRUE);

print_r($data);

echo "<br><br><br><br>";

foreach ($data as $recenttrades) {
    echo "VALUES('{$recenttrades->quantity}', '{$recenttrades->price}' ";
}


?>

Update: but can't get the value from primaryname and primarycode.

I have tried this:

$json_string = 'http://pubapi.cryptsy.com/api.php?method=marketdatav2';

$jsondata = file_get_contents($json_string);

$data = json_decode($jsondata, TRUE);

//print_r($data);


foreach ($data["market"] as $markets) { 
echo "Primary code: <strong>{$markets['primarycode']}</strong><br>";

  foreach($markets as $market) {
    foreach($market as $attributes) {
      foreach($attributes["recenttrades"] as $recenttrade) {

        echo "quantity: " . $recenttrade['quantity'] .", price: " . $recenttrade['price'] . "<br>";
      }
    }
  }
}

5 Answers5

2

recenttrades is nested pretty deeply in that array. Try

foreach ($data['return']['markets']['FST']['recenttrades'] as $recenttrades) {
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
2

recenttrades is several levels nested, so simply doing foreach($data as $recenttrades) is not sufficient.

You need to do:

$recentTrades = $data['return']['markets']['FST']['recenttrades'];

foreach($recentTrades as $recentTrade) {
   ...
}
Ayush
  • 41,754
  • 51
  • 164
  • 239
2

Others have mentioned that you're dealing with nested arrays, not objects. Along with pointing out the issue of being nested rather deeply, I would suggest digging down with foreach (I will probably be crucified for this):

     <?php


$json_string = 'http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=44';

$jsondata = file_get_contents($json_string);

$data = json_decode($jsondata, TRUE);

//print_r($data);

echo "<br><br><br><br>";

foreach ($data as $markets) {
  foreach($markets as $market) {
    foreach($market as $attributes) {
      foreach($attributes["recenttrades"] as $recenttrade) { 
        //echo "<pre>";
        //print_r($recenttrade);
        //echo "</pre>";
        echo "VALUES('{quantity: " . $recenttrade['quantity'] ."}', 'price: {" . $recenttrade['price'] . "}')";
      }
    }
  }
}

?>

This will ensure that you grab every recentrades item at this level of the array. This way you are prepared for other markets to be added to the API and your code isn't locked into searching only in the item named "FST".

Josh Stafford
  • 158
  • 1
  • 6
  • i like this. But how can i get other in the loop i have tried this: – el_capitan Dec 31 '13 at 00:30
  • You can copy that `echo` / `print_r` block that I'm doing on `$recenttrade` and apply it to each level of the `foreach` sequence. That will show you every `$key => $val` pair available on the given level. Does that make sense? – Josh Stafford Jan 06 '14 at 15:38
1

recenttrades is nested deeply and you're asking for arrays, not objects. This seems to work:

foreach ($data['return']['markets']['FST']['recenttrades'] as $recenttrades) {
    echo "VALUES('{$recenttrades['quantity']}', '{$recenttrades['price']}' ";
}
bishop
  • 37,830
  • 11
  • 104
  • 139
0

In response to your update, where you want to loop over markets, try this:

foreach ($data['return']['markets'] as $market) {
  echo "Primary code: <strong>{$market['primarycode']}</strong><br>";
  foreach ($market["recenttrades"] as $recenttrade) {
      echo "quantity: " . $recenttrade['quantity'] .", price: " . $recenttrade['price'] . "<br>";
  }
}
bishop
  • 37,830
  • 11
  • 104
  • 139