使用PHP处理多维JSON数组

时间:2022-11-05 21:28:49

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 myemail@gmail.com worker. I can access the confirmed_reward, hashrate, ipa, and payout_history, but i'm having trouble formatting and outputting the workers array.

这是deepbit.net为我的比特币矿工工作者返回的json。我正在尝试访问workers数组并循环打印myemail@gmail.com worker的统计信息。我可以访问confirmed_reward,hashrate,ipa和payout_history,但是我在格式化和输出workers数组时遇到了问题。

{
 "confirmed_reward":0.11895358,
 "hashrate":236.66666667,
 "ipa":true,
 "payout_history":0.6,
 "workers":
    {
      "myemail@gmail.com":
       {
         "alive":false,
         "shares":20044,
         "stales":51
       }
    }
}

Thank you for your help :)

感谢您的帮助 :)

4 个解决方案

#1


16  

I assume you've decoded the string you gave with json_decode method, like...

我假设你用json_decode方法解码了你给的字符串,比如......

$data = json_decode($json_string, TRUE);

To access the stats for the particular worker, just use...

要访问特定工作人员的统计数据,只需使用...

$worker_stats = $data['workers']['myemail@gmail.com'];

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

例如,为了检查它是否还活着,你可以选择......

$is_alive = $worker_stats['alive'];

It's really that simple. )

这真的很简单。 )

#2


3  

Why don't you use json_decode.

你为什么不用json_decode。

You pass the string and it returns an object/array that you will use easily than the string directly.

你传递了字符串,它返回一个你将比字符串直接使用的对象/数组。

To be more precise :

更确切地说:

<?php
$aJson = json_decode('{"confirmed_reward":0.11895358,"hashrate":236.66666667,"ipa":true,"payout_history":0.6,"workers":{"myemail@gmail.com":{"alive":false,"shares":20044,"stales":51}}}');
$aJson['workers']['myemail@gmail.com']; // here's what you want!
?>

#3


2  

$result = json_decode($json, true); // true to return associative arrays
                                    // instead of objects

var_dump($result['workers']['myemail@gmail.com']);

#4


2  

You can use json_decode to get an associative array from the JSON string.

您可以使用json_decode从JSON字符串中获取关联数组。

In your example it would look something like:

在您的示例中,它看起来像:

$json = 'get yo JSON';
$array = json_decode($json, true); // The `true` says to parse the JSON into an array,
                                   // instead of an object.
foreach($array['workers']['myemail@gmail.com'] as $stat => $value) {
  // Do what you want with the stats
  echo "$stat: $value<br>";
}

#1


16  

I assume you've decoded the string you gave with json_decode method, like...

我假设你用json_decode方法解码了你给的字符串,比如......

$data = json_decode($json_string, TRUE);

To access the stats for the particular worker, just use...

要访问特定工作人员的统计数据,只需使用...

$worker_stats = $data['workers']['myemail@gmail.com'];

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

例如,为了检查它是否还活着,你可以选择......

$is_alive = $worker_stats['alive'];

It's really that simple. )

这真的很简单。 )

#2


3  

Why don't you use json_decode.

你为什么不用json_decode。

You pass the string and it returns an object/array that you will use easily than the string directly.

你传递了字符串,它返回一个你将比字符串直接使用的对象/数组。

To be more precise :

更确切地说:

<?php
$aJson = json_decode('{"confirmed_reward":0.11895358,"hashrate":236.66666667,"ipa":true,"payout_history":0.6,"workers":{"myemail@gmail.com":{"alive":false,"shares":20044,"stales":51}}}');
$aJson['workers']['myemail@gmail.com']; // here's what you want!
?>

#3


2  

$result = json_decode($json, true); // true to return associative arrays
                                    // instead of objects

var_dump($result['workers']['myemail@gmail.com']);

#4


2  

You can use json_decode to get an associative array from the JSON string.

您可以使用json_decode从JSON字符串中获取关联数组。

In your example it would look something like:

在您的示例中,它看起来像:

$json = 'get yo JSON';
$array = json_decode($json, true); // The `true` says to parse the JSON into an array,
                                   // instead of an object.
foreach($array['workers']['myemail@gmail.com'] as $stat => $value) {
  // Do what you want with the stats
  echo "$stat: $value<br>";
}