Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing Multidimensional JSON Array with PHP

Tags:

json

php

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

like image 745
0xDECAFBAD Avatar asked Feb 27 '12 20:02

0xDECAFBAD


People also ask

How to get data from Multidimensional JSON array in PHP?

Firstly read the contents of the text file into a string variable using the file_get_contents() function and then use json_decode() function to convert the JSON string to a PHP variable. $filepath = './persons. txt'; $json_string = file_get_contents($filepath); $json = json_decode($json_string, true);

How do you loop through a multidimensional array in PHP?

Looping through multidimensional arrays Just as with regular, single-dimensional arrays, you can use foreach to loop through multidimensional arrays. To do this, you need to create nested foreach loops — that is, one loop inside another: The outer loop reads each element in the top-level array.

How to parse JSON using PHP?

Reading JSON From a File or String in PHP First, you need to get the data from the file into a variable by using file_get_contents() . Once the data is in a string, you can call the json_decode() function to extract information from the string.

How to build JSON array in PHP?

PHP File explained:Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function.


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

like image 139
raina77ow Avatar answered Oct 21 '22 15:10

raina77ow