PHP访问阵列键内的数组

时间:2018-01-10 18:51:30

标签: php api

我正在尝试从Binance Exchange API(https://api.etherdelta.com/returnTicker)获取数据。我能够获取数据并将数据转换为数组。但是整个数据是数组第一个数据的关键。

$uri  = "https://api.etherdelta.com/returnTicker";
$data = array();
$ch = curl_init ($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
parse_str(curl_exec($ch), $data);

示例数据:

Array ( [{"ETH_VIBE":{"tokenAddr":"0xe8ff5c9c75deb346acac493c463c8950be03dfba","quoteVolume":2022554_248,"baseVolume":3212_187,"last":0_001659444,"percentChange":3_2991,"bid":5,"ask":0_00163},"ETH_0xc27a":{"tokenAddr":"0xc27a2f05fa577a83ba0fdb4c38443c0718356501","quoteVolume":1379508_983,"baseVolume":1262_329,"last":0_000911962,"percentChange":-0_0685,"bid":0_0009785,"ask":0_00093}])

我希望访问每个令牌的最后价格(ETH_VIBE)。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我认为您需要做的就是将其解析为json字符串。此外,你应该检查curl_exec($ ch)实际上是否返回了一个字符串而不是意外的东西。 http://php.net/manual/en/function.json-decode.php http://php.net/manual/en/function.curl-exec.php

<?php
...
// Set 2nd param to TRUE to return an array
$data = json_decode(curl_exec($ch), true);