循环复杂的json对象php

时间:2018-05-23 11:23:04

标签: php json

好的,我必须从nanopool循环一个json对象。

我的PHP代码获取json并解码它:

$json = file_get_contents("https://api.nanopool.org/v1/eth/payments/0x218494b2284a5f165ff30d097d3d7a542ff0023B");

$decode =  json_decode($json);

json var_dump如下:

    object(stdClass)#1 (2){
  [
    "status"
  ]  => bool(true)  [
    "data"
  ]  => array(9)  {
    [
      0
    ]    => object(stdClass)#2 (4)    {
      [
        "date"
      ]      => int(1526913390)      [
        "txHash"
      ]      => string(66) "0xd55f83fd04fba6bf47f77494c2348417ae892199ec5a3037d5731acdf4b51c53"      [
        "amount"
      ]      => float(1.00284502876)      [
        "confirmed"
      ]      => bool(true)
    }    [
      1
    ]    => object(stdClass)#3 (4)    {
      [
        "date"
      ]      => int(1526625835)      [
        "txHash"
      ]      => string(66) "0x65e26221a69acc7b08bdda873c55620fad8f7275cde8064a02866128197f8d33"      [
        "amount"
      ]      => float(1.00043622303)      [
        "confirmed"
      ]      => bool(true)
    }    [
      2
    ]    => object(stdClass)#4 (4)    {
      [
        "date"
      ]      => int(1526339866)      [
        "txHash"
      ]      => string(66) "0xc574992e6c4620d6da3430d5902fde4d600e38d6973333ad44f6e1c398558834"      [
        "amount"
      ]      => float(1.00010024694)      [
        "confirmed"
      ]      => bool(true)
    }    [
      3
    ]    => object(stdClass)#5 (4)    {
      [
        "date"
      ]      => int(1526040073)      [
        "txHash"
      ]      => string(66) "0x71abfbfe6c18f0e200d4fb07101adc868af5e38e670d00806576ec24023b698d"      [
        "amount"
      ]      => float(1.00028562626)      [
        "confirmed"
      ]      => bool(true)
    }    [
      4
    ]    => object(stdClass)#6 (4)    {
      [
        "date"
      ]      => int(1525753141)      [
        "txHash"
      ]      => string(66) "0xf3f955d88a65fa2461a99bba8940105d070c28e925498f56cb9606c608eee354"      [
        "amount"
      ]      => float(1.00124398757)      [
        "confirmed"
      ]      => bool(true)
    }    [
      5
    ]    => object(stdClass)#7 (4)    {
      [
        "date"
      ]      => int(1525456317)      [
        "txHash"
      ]      => string(66) "0xfff92a0f9619e985f58cc0d12b9b21cf3602ff4b5d77c1a32b4470b65862157e"      [
        "amount"
      ]      => float(1.00446854575)      [
        "confirmed"
      ]      => bool(true)
    }    [
      6
    ]    => object(stdClass)#8 (4)    {
      [
        "date"
      ]      => int(1525160982)      [
        "txHash"
      ]      => string(66) "0x0099dbe21fa4efa9eab9a449b67d093b5ddc84762e8a9ea82a8e9f67289e3922"      [
        "amount"
      ]      => float(0.202524366365)      [
        "confirmed"
      ]      => bool(true)
    }    [
      7
    ]    => object(stdClass)#9 (4)    {
      [
        "date"
      ]      => int(1525097578)      [
        "txHash"
      ]      => string(66) "0x73e902ae5359a3f8201ad685e00960c53c89820947d2a97a9246252c0c0bbe1a"      [
        "amount"
      ]      => float(0.202628621518)      [
        "confirmed"
      ]      => bool(true)
    }    [
      8
    ]    => object(stdClass)#10 (4)    {
      [
        "date"
      ]      => int(1525012980)      [
        "txHash"
      ]      => string(66) "0xced7a9054c22206eed4243b5f9575b0cee564675f6a75c32cc9634571a01ec46"      [
        "amount"
      ]      => float(0.20110870859)      [
        "confirmed"
      ]      => bool(true)
    }
  }
}

现在我想循环遍历所有json并使用以下代码获取每个“记录”的日期和数量:

foreach($decode as $item) { 
    $date = $item['date'];
    $amount = $item['amount'];
}

但它不起作用。 有关此问题的任何帮助,以便正确提取所需数据,这是非常受欢迎的

任何帮助?

1 个答案:

答案 0 :(得分:2)

使用true作为json_decode()的第二个参数,将json数据转换为普通PHP数组

$decode =  json_decode($json,true);

现在迭代它并获取数据

foreach($decode['data'] as $val){
   echo date('Y-m-d',$val['date']).'--'.$val['amount'];
   echo "<br/>";
}

输出: - https://prnt.sc/jlkak3