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>";
      }
    }
  }
}
3
  • I might be wrong, but if 2nd argument = true, you get an associative array. But there you're trying to access the values from an object Commented Dec 30, 2013 at 20:35
  • Maybe this help stackoverflow.com/questions/15552385/… Commented Dec 30, 2013 at 20:36
  • Remove TRUE if you want an object to be returned. Commented Dec 30, 2013 at 20:52

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

i like this. But how can i get other in the loop i have tried this:
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?
2

recenttrades is nested pretty deeply in that array. Try

foreach ($data['return']['markets']['FST']['recenttrades'] as $recenttrades) {

Comments

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) {
   ...
}

Comments

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']}' ";
}

Comments

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>";
  }
}

Comments

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.