如何通过php从嵌套数组中获取API数据

时间:2018-05-25 12:49:39

标签: php json api

我想按照以下代码从数组中的数组中获取数据:

{"address":"0x64890972513558cb4e4b4a7f8af48d892fd097b7","ETH":{"**balance**":0,"totalIn":0,"totalOut":0},"countTxs":0,"tokens":[{"**tokenInfo**":{"**address**":"0xad640689e6950b7453729a4686edb3fdfd754616","**name**":"CIChain","decimals":"18","symbol":"CIC","totalSupply":"3000000000000000000000000000","owner":"0xecb64b92ffb5179b47c87d2f105d6eeebb132c68","lastUpdated":1527246490,"issuancesCount":0,"holdersCount":31528,"**price**":false}

我正在使用这些步骤,但我无法继续:

    $address = "0x64890972513558cb4e4b4a7f8af48d892fd097b7"; //$_POST['address'];


$scan_url = 'https://api.ethplorer.io/getAddressInfo/'.$address.'?apiKey=freekey';
$scan_json = file_get_contents($scan_url);
$scan_array = json_decode($scan_json, true);
foreach($scan_array['tokens'] as $key => $eth) { 
    foreach($eth['tokenInfo'] as $key => $etth) {  

  echo $etth['name'];  
}}

我想通过明星**将标记日期检索到php中的echo中,以便如何获取嵌套数据。

1 个答案:

答案 0 :(得分:1)

根据您提供的网址,您需要更改以下代码

$scan_array = json_decode($scan_json, true);
foreach($scan_array['tokens'] as $key => $eth) { 
    echo $eth['tokenInfo']["name"]. "\n";
}

Live demo

第二个foreach正在提供来自tokenInfo的所有元素,因此无需使用内部foreach或者不需要像$etth['name']那样使用所有元素,仅{ {1}}就足够了。

相关问题