dongmu1989 2019-03-29 13:07 采纳率: 0%
浏览 44
已采纳

比较未以数字格式列出的JSON API中的数据

PHP beginner here. I want to display compared Data ($myCoins) from a JSON API, so that only those Items will be displayed that I first inserted into the $myCoins arrays, but I can't figure out how to get the code to operate the same with NAMES instead of NUMBERS.

Used API:

https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRB,MIOTA,XRP,XLM,TRX&tsyms=USD

CODE:

<?php
//// API
$coinData = json_decode(file_get_contents('https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRB,IOTA,XRP,XLM,TRX,LINK&tsyms=USD'), true); 

// Arrays
$myCoins = array(
   'BTC' => array ( 'balance' => 12.90 ),
   'ETH' => array ( 'balance' => 122.23 ),
   'XRB' => array ( 'balance' => 221.52 ),
   'MIOTA' => array ('balance' => 233.00 ),
   'XRP' => array ( 'balance' => 429.00 ),
   'XLM' => array ( 'balance' => 1205.89 ),
   'TRX' => array ( 'balance' => 5299.40 )
);

// Fetch the Coins
$numCoins = sizeof ($coinData['RAW']);
$portfolioValue = 0;
for ( $xx=0; $xx<$numCoins; $xx++) {
   // Comparing to Data
   $thisCoinSymbol = $coinData[$xx]['FROMSYMBOL'];
   // 
   $coinHeld = array_key_exists($thisCoinSymbol, $myCoins);
   // Only retour held
    if ( !$coinHeld ) { continue; }

      // names test:
      echo $coinData[$xx]['FROMSYMBOL'];
};
?>

The Tutorial I used was made with an API that displays the Items as numbers, not the Item names like in the API I want to use.

Tutorial API:

https://api.coinmarketcap.com/v1/ticker/

So my console puts out a Undefined offset: 0 - Undefined offset: 7 on line $thisCoinSymbol = $coinData[$xx]['FROMSYMBOL']; I understand that this is because $xx equals 0 - 7, because there are 8 Items under ['RAW'], and there is no 0-7 in the API I use.

How would i get the same result as the Tutorial but with the API I want to use?

  • 写回答

1条回答 默认 最新

  • doudong8713 2019-03-29 13:28
    关注

    You should use a foreach to parse your json that is structured like that:

    RAW: {
      BTC: {
        USD: {
          TYPE: "5",
          MARKET: "CCCAGG",
          FROMSYMBOL: "BTC", 
          ....
        }
      },
      ETH: {....},
      ...
    }
    

    So your code should looks like that:

    <?php
    
     $coinData = json_decode(file_get_contents('https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRB,IOTA,XRP,XLM,TRX,LINK&tsyms=USD'), true);
    
     $myCoins = array(
      'BTC' => array ( 'balance' => 12.90 ),
      'ETH' => array ( 'balance' => 122.23 ),
      'XRB' => array ( 'balance' => 221.52 ),
      'MIOTA' => array ('balance' => 233.00 ),
      'XRP' => array ( 'balance' => 429.00 ),
      'XLM' => array ( 'balance' => 1205.89 ),
      'TRX' => array ( 'balance' => 5299.40 )
    );
    
    $portfolioValue = 0;
    
    // your information in json path ['RAW'] so safeguard here to be sure it exists
    if (isset($coinData['RAW'])) {
      // then loop on all entries $cryptoSymbol will contains for example BTC and cryptoInfo the array USD => [...]
      foreach($coinData['RAW'] as $cryptoSymbol => $cryptoInfo) {
         // safeguard, check path [USD][FROMSYMBOL] exists
         if (!isset($cryptoInfo['USD']) || !isset($cryptoInfo['USD']['FROMSYMBOL'])) {
            // log or do whatever to handle error here
            echo "no path [USD][FROMSYMBOL] found for crypto: " . $cryptoSymbol . PHP_EOL;
            continue;
         }
    
         // Your symbol in on your json path/array [USD][FROMSYMBOL]
         $thisCoinSymbol = $cryptoInfo['USD']['FROMSYMBOL'];
         $coinHeld = array_key_exists($thisCoinSymbol, $myCoins);
         // Only retour held
         if ( !$coinHeld ) { continue; }
    
         echo $cryptoInfo['USD']['FROMSYMBOL'] . PHP_EOL;
     }
    }
    ?>
    

    You will have the output:

    BTC ETH XRB XRP XLM TRX

    Now you can parse the json in the right way, it should be easy for you to build the expected json.

    Have a look at the foreach doc here: https://www.php.net/manual/en/control-structures.foreach.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作