9

This is the json that deepbit.net returns for my Bitcoin Miner worker. I'm trying to access the workers array and loop through to print the stats for my [email protected] worker. I can access the confirmed_reward, hashrate, ipa, and payout_history, but i'm having trouble formatting and outputting the workers array.

{
 "confirmed_reward":0.11895358,
 "hashrate":236.66666667,
 "ipa":true,
 "payout_history":0.6,
 "workers":
    {
      "[email protected]":
       {
         "alive":false,
         "shares":20044,
         "stales":51
       }
    }
}

Thank you for your help :)

0

4 Answers 4

21

I assume you've decoded the string you gave with json_decode method, like...

$data = json_decode($json_string, TRUE);

To access the stats for the particular worker, just use...

$worker_stats = $data['workers']['[email protected]'];

To check whether it's alive, for example, you go with...

$is_alive = $worker_stats['alive'];

It's really that simple. )

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

1 Comment

Thanks man, json_decode($json_string, TRUE) did the trick. Second parameter did the magic!
4

You can use json_decode to get an associative array from the JSON string.

In your example it would look something like:

$json = 'get yo JSON';
$array = json_decode($json, true); // The `true` says to parse the JSON into an array,
                                   // instead of an object.
foreach($array['workers']['[email protected]'] as $stat => $value) {
  // Do what you want with the stats
  echo "$stat: $value<br>";
}

Comments

3

Why don't you use json_decode.

You pass the string and it returns an object/array that you will use easily than the string directly.

To be more precise :

<?php
$aJson = json_decode('{"confirmed_reward":0.11895358,"hashrate":236.66666667,"ipa":true,"payout_history":0.6,"workers":{"[email protected]":{"alive":false,"shares":20044,"stales":51}}}');
$aJson['workers']['[email protected]']; // here's what you want!
?>

Comments

2
$result = json_decode($json, true); // true to return associative arrays
                                    // instead of objects

var_dump($result['workers']['[email protected]']);

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.