Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP处理多维JSON数组_Php_Json - Fatal编程技术网

用PHP处理多维JSON数组

用PHP处理多维JSON数组,php,json,Php,Json,这是deepbit.net为我的比特币矿工工人返回的json。我正在尝试访问workers数组并循环以打印我的数据库的统计数据myemail@gmail.com工人我可以访问已确认的奖金、hashrate、ipa和支出历史记录,但我在格式化和输出workers数组时遇到问题 { "confirmed_reward":0.11895358, "hashrate":236.66666667, "ipa":true, "payout_history":0.6, "workers":

这是deepbit.net为我的比特币矿工工人返回的json。我正在尝试访问workers数组并循环以打印我的数据库的统计数据myemail@gmail.com工人我可以访问已确认的奖金、hashrate、ipa和支出历史记录,但我在格式化和输出workers数组时遇到问题

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

为什么不使用

传递字符串后,它将返回一个对象/数组,您将比直接使用字符串更容易使用该对象/数组

更准确地说:

<?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!
?>

我假设您已经用方法解码了您给出的字符串,比如

$data = json_decode($json_string, TRUE);
要访问特定工作人员的统计信息,只需使用

$worker_stats = $data['workers']['myemail@gmail.com'];
例如,为了检查它是否还活着,你可以用

$is_alive = $worker_stats['alive'];
其实就这么简单。)

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

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

$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>";
}
$json='get yo json';
$array=json_decode($json,true);//'true'表示将JSON解析为数组,
//而不是一个对象。
foreach($array['workers']['myemail@gmail.com“]作为$stat=>$value){
//用统计数据做你想做的
echo“$stat:$value
”; }
谢谢,伙计,json_解码($json_字符串,TRUE)成功了。第二个参数发挥了神奇的作用!
$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>";
}