Home » Php » Processing Multidimensional JSON Array with PHP

Processing Multidimensional JSON Array with PHP

Posted by: admin July 12, 2020 Leave a comment


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.

      "[email protected]":

Thank you for your help 🙂

How to&Answers:

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


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 :

$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!


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


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

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