[英]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".我如何拉工人的价值,我想要结果“iamdamnsam.L3”。 I tried
$data->workers[0];
我试过
$data->workers[0];
but that didn't work.但这没有用。 I am using
json_decode()
to get $data
我正在使用
json_decode()
来获取$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 )
You could access to a property with special chars using {"name"}
:您可以使用
{"name"}
访问具有特殊字符的属性:
// example for access to connected property of `iamdamnsam.L3`
$data->user->workers->{"iamdamnsam.L3"}->connected
Simple example:简单的例子:
$data = json_decode('{"a":{"b.2":{"c":3}}}');
var_dump($data->a->{"b.2"}->c); // int(3)
# or with a variable:
$k = "b.2";
var_dump($b->a->$k->c); // int(3)
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.然后,您可以使用Type Casting即时将 Object 转换为数组,从而循环所有工作人员的 object。
(array)$object
Start loop on $object->user->workers
and then you iterate each worker one by one.在
$object->user->workers
上开始循环,然后逐个迭代每个工作人员。
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
: workers
钥匙 Output :
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: foreach
循环的Output:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.