PHP实现基于MarketID合并两个API的JSON数据并按基础货币分组
Merging API Data & Grouping by Base Currency
I’ll help you combine the two API calls, map prices to their respective markets, and display everything neatly grouped by the base currency. Here’s the revised PHP script with clear explanations:
<?php include 'pappu.php'; // Disable caching to ensure fresh API data header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); // Step 1: Fetch and store all market details (ID, trading pair, base currency) $marketsData = ngegrab('https://www.coinexchange.io/api/v1/getmarkets'); $marketsJson = json_decode($marketsData); $markets = []; if ($marketsJson && isset($marketsJson->result)) { foreach ($marketsJson->result as $market) { $marketId = $market->MarketID; $markets[$marketId] = [ 'baseCurrency' => $market->BaseCurrencyCode, 'tradingPair' => "{$market->MarketAssetCode}/{$market->BaseCurrencyCode}", 'price' => 'N/A' // Fallback if price isn't found later ]; } } // Step 2: Fetch market summaries and update prices for existing markets $summariesData = ngegrab('https://www.coinexchange.io/api/v1/getmarketsummaries'); $summariesJson = json_decode($summariesData); if ($summariesJson && isset($summariesJson->result)) { foreach ($summariesJson->result as $summary) { $marketId = $summary->MarketID; if (isset($markets[$marketId])) { $markets[$marketId]['price'] = $summary->LastPrice; } } } // Step 3: Group markets by their base currency $groupedByBase = []; foreach ($markets as $marketId => $info) { $baseCurrency = $info['baseCurrency']; if (!isset($groupedByBase[$baseCurrency])) { $groupedByBase[$baseCurrency] = []; } $groupedByBase[$baseCurrency][] = [ 'marketId' => $marketId, 'tradingPair' => $info['tradingPair'], 'price' => $info['price'] ]; } // Sort base currencies alphabetically for cleaner display ksort($groupedByBase); // Step 4: Display the grouped results foreach ($groupedByBase as $base => $entries) { echo "<h2>Base Currency: $base</h2>"; echo "<ul>"; foreach ($entries as $entry) { echo "<li>"; echo "MarketID: {$entry['marketId']} | "; echo "Trading Pair: {$entry['tradingPair']} | "; echo "Last Price: {$entry['price']}"; echo "</li>"; } echo "</ul>"; } ?>
Key Improvements & Explanations:
- Structured Data Storage: We first store all market details in an array indexed by
MarketID—this makes it trivial to look up and update prices later when we fetch the summaries. - Price Mapping: We only update prices for markets we already have data for, avoiding orphaned entries from incomplete summary data.
- Grouping by Base Currency: We reorganize the data into a new array where each key is a base currency (like BTC, LTC), and the value is a list of all markets using that base currency.
- Clean Display: We sort base currencies alphabetically and use HTML headings/lists to make the output easy to scan.
- Fallback for Missing Prices: If a market doesn’t have a price in the summaries, we show "N/A" instead of leaving it blank.
内容的提问来源于stack exchange,提问作者Praburam
相关产品推荐
相关产品推荐

