0

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.

The array is generated here: https://fapi.binance.com/fapi/v1/exchangeInfo So I am doing the following:

    $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. $data[0]['symbols'].. to $data[9]['symbols'] etc. but can't seem to get the correct array offset.

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"

Ty..

2
  • 2
    It's hard to answer this when you've only posted a partial of the array structure. Without knowing the full structure, we can only make unfounded guesses. Commented Jan 31, 2022 at 15:33
  • try to decode the json response you get from the CURL, $data = json_decode($data);. and now $data is an object that you can have accesses like $data->symbols Commented Jan 31, 2022 at 15:43

3 Answers 3

1

It doesn't work because the CURL result is in JSON.

You got to convert it to an array before you can loop through it.

$data = json_decode($data, true);
Sign up to request clarification or add additional context in comments.

2 Comments

Warning: json_decode() expects parameter 1 to be string, array given ...
@metokitt the $data in your original question is a string, this works with that code.
1

It looks like you need to convert the JSON response into an array before you loop over it.

    $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);

2 Comments

Warning: Invalid argument supplied for foreach() in /data/www/.....
My bad, I posted the concept without testing. I have updated the answer for you now.
0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.