Skip to content
Advertisement

How do you pull this value out of a stdClass Object?

How do I pull the value of the workers, I want the result “iamdamnsam.L3”. I tried $data->workers[0]; but that didn’t work. I am using json_decode() to get $data

stdClass Object ( 
        [user] => stdClass Object ( 
                [hash_rate] => 5994054 
                [expected_24h_rewards] => 0.37416681589133 
                [total_rewards] => 45.048671139143 
                [paid_rewards] => 45.01 
                [unpaid_rewards] => 0.038671139142861  
                [past_24h_rewards] => 0.3643237698601 
                [total_work] => 57954156777832448 
                [blocks_found] => 2 ) 
                [workers] => stdClass Object ( 
                        [iamdamnsam.L31] => stdClass Object ( 
                                [connected] => 1 
                                [hash_rate] => 611723.1 
                                [hash_rate_24h] => 502844.2 
                                [valid_shares] => 59142307840 
                                [stale_shares] => 62652416 
                                [invalid_shares] => 36 
                                [rewards] => 2.9337096738472 
                                [rewards_24h] => 0.03134870470656 
                                [last_share_time] => 1642707957 
                                [reset_time] => 1632875969 )

Advertisement

Answer

Let’s say there are/will be more workers.

Then you can loop the object of all workers by converting the Object to the array on the fly using Type Casting ie. (array)$object

Start loop on $object->user->workers and then you iterate each worker one by one.

See example bellow:

<?php

// Note I have added one more worker "iamdamnsam.L42"
$json = '{"user":{"hash_rate":5994054,"expected_24h_rewards":0.37416681589133,"total_rewards":45.048671139143,"paid_rewards":45.01,"unpaid_rewards":0.038671139142861,"past_24h_rewards":0.3643237698601,"total_work":57954156777832448,"blocks_found":2,"workers":{"iamdamnsam.L31":{"connected":1,"hash_rate":611723.1,"hash_rate_24h":502844.2,"valid_shares":59142307840,"stale_shares":62652416,"invalid_shares":36,"rewards":2.9337096738472,"rewards_24h":0.03134870470656,"last_share_time":1642707957,"reset_time":1632875969},"iamdamnsam.L42":{"connected":1,"hash_rate":321645.1,"hash_rate_24h":987654.2}}}}';


// Get the object from JSON string
$object = json_decode($json);


// Print the actual object from JSON data
print_r($object);


// Get list of 'worker' key names
print_r(array_keys((array)$object->user->workers));

Output of keys of workers:

Array
(
    [0] => iamdamnsam.L31
    [1] => iamdamnsam.L42
)

Looping all the workers and accessing their properties:

foreach ((array)$object->user->workers as $key => $values){
    echo 'Key name: ' . $key;
    echo ', count of values: ' . count((array)$values);
    echo ', hash rate full path: ' . $object->user->workers->{$key}->hash_rate;
    echo ', hash rate relative path: ' . $values->hash_rate; // same as above
    echo PHP_EOL;
}

Output of foreach loop:

Key name: iamdamnsam.L31, count of values: 10, hash rate full path: 611723.1, hash rate relative path: 611723.1
Key name: iamdamnsam.L42, count of values: 3, hash rate full path: 321645.1, hash rate relative path: 321645.1

Live demo

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement