【问题标题】:How do you pull this value out of a stdClass Object?你如何从 stdClass 对象中提取这个值?
【发布时间】:2022-01-20 20:07:49
【问题描述】:

我如何提取工人的价值,我想要结果“iamdamnsam.L3”。 我试过$data->workers[0];,但没用。我正在使用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 )

【问题讨论】:

  • array_flip(get_object_vars($data->user->workers))[0]
  • 更清楚一点,iamdamnsam.L31 是矿工的名字,有很多。所以我试图拉他们的名字。所以对于结果,我想要iamdamnsam.L31、iamdamnsam.L32等。我没有把整个数组放在这里以节省空间。

标签: php json


【解决方案1】:

您可以使用 {"name"} 访问带有特殊字符的属性:

// example for access to connected property of `iamdamnsam.L3`
$data->user->workers->{"iamdamnsam.L3"}->connected

简单示例:

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

【讨论】:

    【解决方案2】:

    假设有/将会有更多个工人。

    然后,您可以通过使用Type Casting 即时将对象转换为数组来循环所有工作人员的对象。 (array)$object

    $object->user->workers 上开始循环,然后逐个迭代每个worker。

    见下面的例子:

    <?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));
    

    workers的键输出:

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

    循环所有工作人员并访问他们的属性:

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

    foreach 循环的输出:

    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

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 1970-01-01
      • 2021-12-25
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      相关资源
      最近更新 更多