Viewed   61 times

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


Thank you for your help :)



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'][''];

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

$is_alive = $worker_stats['alive'];

It's really that simple. )

Saturday, September 10, 2022

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022

Your $data_drugs is no longer a json, after json_decode is an associative array.
You don't need any loop to see keys and values

$data_drugs = json_decode($json_drugs,true);

/* or if you don't like inline */


You can use var_dump($data_drugs) - keys and values with types, probably you don't need this
But if you want to display keys and values more ...fancy use a recursive function

function show($x){
    foreach($x as $key=>$val){
        echo"<p>$key : ";
        if(is_array($val)){ echo". . ."; show($val);}
        else{ echo"$val</p>";}}}

Saturday, October 15, 2022

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
    echo "true";    
else {
    echo "false";   
Thursday, October 6, 2022

If the output of value.producer is an object, then you should be able to access the properties of that object. For example, should output 'asdfsadf'.


It looks as though your json data is not valid. (I'm assuming "producers" is a property of the data object and the value of that property is an array of all of the "producer" objects.)

I've created this fiddle ( ) and it works as expected. I just had to change the comma to a colon after "producers":

"producers":  // changed from "producers",
Sunday, October 30, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :