简体   繁体   English

如何正确解析这个多维数组

[英]How to parse this multi-dimensional array properly

I have tried everything I know and I have failed totally to parse this multi dimensional array so that I can get the "symbol" keys and corresponding values of "contractType", but I can't seem to get it.我已经尝试了我所知道的一切,但我完全无法解析这个多维数组,以便我可以获得“contractType”的“symbol”键和相应的值,但我似乎无法得到它。

The array is generated here: https://fapi.binance.com/fapi/v1/exchangeInfo So I am doing the following:数组在这里生成: https://fapi.binance.com/fapi/v1/exchangeInfo所以我正在做以下事情:

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    $data = curl_exec($curl);
    curl_close($curl);

    $results = [];
    foreach ($data['symbols'] as $info) {
        $results[] = $info['symbol'];        
    }
    print_r($results);

I tried a foreach loop, for loop, tried various offsets eg.我尝试了一个 foreach 循环,for 循环,尝试了各种偏移量,例如。 $data[0]['symbols'].. to $data[9]['symbols'] etc. but can't seem to get the correct array offset. $data[0]['symbols'].. 到 $data[9]['symbols'] 等,但似乎无法获得正确的数组偏移量。

10000"}],"symbols":[{"symbol":"BTCUSDT","pair":"BTCUSDT","contractType":"PERPETUAL","deliveryDate 
  • I'm trying to loop through the "symbols" offset within this main array, and get 1. the "symbol" 2. its corresponding "contractType"我正在尝试遍历此主数组中的“符号”偏移量,并获取 1.“符号”2.其对应的“contractType”

Ty..泰..

It doesn't work because the CURL result is in JSON.它不起作用,因为 CURL 结果在 JSON 中。

You got to convert it to an array before you can loop through it.您必须先将其转换为数组,然后才能循环遍历它。

$data = json_decode($data, true);

It looks like you need to convert the JSON response into an array before you loop over it.看起来您需要在循环之前将 JSON 响应转换为数组。

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    $data = curl_exec($curl);
    curl_close($curl);

    $results = [];
    $data = json_decode($data);
    $i = 0;
    foreach ($data->symbols as $info) {
        $results[$i]['symbol'] = $info->symbol;
        $results[$i]['contractType'] = $info->contractType;
        $i++;    
    }
    print_r($results);

Yes.是的。 Thankyou.. (didn't understand answer properly.)谢谢..(没有正确理解答案。)

    $url = 'https://fapi.binance.com/fapi/v1/exchangeInfo';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    $data = curl_exec($curl);
    curl_close($curl);

    $data = json_decode($data, true);

    $results = [];
    $symbols = $data['symbols'];
    $i = 0;
    foreach ($symbols as $info) {
        $results[$i]['symbol'] = $info['symbol'];
        $results[$i]['contractType'] = $info['contractType'];
        $i++;    
    }
    print_r($results);

Much appreciated, ty..非常感谢,泰..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM