我怎样才能连接 2 .json url's?

How can I concatenate 2 .json url's?

我可以使用什么功能将 2 .json 数据连接在一起?

基本上我有:

https://api.coinmarketcap.com/v2/ticker/?limit=1&start=1https://api.coinmarketcap.com/v2/ticker/?limit=1&start=2

我想将 "data" 段连接在一起,所以它看起来像是连接的 我可以使用什么 php 函数?这样我就可以了解一下了。

$sources = array(1,2);
foreach ($sources as $sourcenum) {
  $source = "https://api.coinmarketcap.com/v2/ticker/?limit=2&start=".$sourcenum;
  $source_json = file_get_contents($source);
  $source_array = json_decode($source_json, TRUE);
  #I'm stuck here. How would I copy all the data and chuck it into my own file  called www.example.com/concatenated_data
#then keep concatenating in the next loop 
}

然后在我的 concatenated_data 中它看起来像:

{
    "data": {
        "1": {
            "id": 1, 
            "name": "Bitcoin", 
            "symbol": "BTC", 
            "website_slug": "bitcoin", 
            "rank": 1, 
            "circulating_supply": 17309562.0, 
            "total_supply": 17309562.0, 
            "max_supply": 21000000.0, 
            "quotes": {
                "USD": {
                    "price": 6576.52005708, 
                    "volume_24h": 3304276557.4679, 
                    "market_cap": 113836681672.0, 
                    "percent_change_1h": -0.52, 
                    "percent_change_24h": -0.33, 
                    "percent_change_7d": -0.52
                }
            }, 
            "last_updated": 1538915062
        }, 
        "1027": {
            "id": 1027, 
            "name": "Ethereum", 
            "symbol": "ETH", 
            "website_slug": "ethereum", 
            "rank": 2, 
            "circulating_supply": 102421710.0, 
            "total_supply": 102421710.0, 
            "max_supply": null, 
            "quotes": {
                "USD": {
                    "price": 223.312150803, 
                    "volume_24h": 1527251449.77882, 
                    "market_cap": 22872012419.0, 
                    "percent_change_1h": -0.14, 
                    "percent_change_24h": -1.19, 
                    "percent_change_7d": -4.63
                }
            }, 
            "last_updated": 1538915014
        }
    }, 
}

这就是您想要的,只需创建一个 $result 数组变量并将所有响应推送给它。就这样:)

<?php
$sources = array(1,2);
$result = [];
foreach ($sources as $sourcenum) {
  $source = "https://api.coinmarketcap.com/v2/ticker/?limit=2&start=$sourcenum";
  $source_json = file_get_contents($source);
   $result[] = json_decode($source_json, TRUE); // push the response to $result
}

echo '<pre>';
print_r($result); // just for debug purpose
echo '</pre>';
echo json_encode($result);
?>

已编辑: 根据 OP 新要求

<?php
$sources = array(1,2);
$result = $required =  [];
foreach ($sources as $sourcenum) {
    $source = "https://api.coinmarketcap.com/v2/ticker/?limit=2&start=$sourcenum";
    $source_json = file_get_contents($source);
    $source_array = json_decode($source_json, TRUE);
    $result[] = $source_array['data'];
}
//print_r($result); // comment out to see what is inside it
foreach($result as $k=>$v){
    foreach($v as $key=>$value){
        $required[$value['name']] = $value['quotes']['USD']['price'];
    }
}
//print_r($required); // comment out to see what is inside it
$mydata=json_encode($required);
file_put_contents("mydata.txt",$mydata); // assumet mydata.txt is on the same directory
?>

如果我理解正确的话,这应该可以满足您的需求:

$sources = array(1,2);
$target = ["data": []]; // pre-build the target output as blueprint
foreach ($sources as $sourcenum) {
  $source = "https://api.coinmarketcap.com/v2/ticker/?limit=2&start=$sourcenum";
  $source_json = file_get_contents($source);
  $source_array = json_decode($source_json, TRUE);
  // add the (first and only?) found data item with it's key to the pre-built target
  $keys = array_keys($source_array['data']);
  $target['data'][$keys[0]] = $source_array['data'][$keys[0]];
}
echo json_encode($target);

// OUTPUT
{
    "data": {
        "1027": {
            "id": 1027,
            "name": "Ethereum",
            "symbol": "ETH",
            "website_slug": "ethereum",
            "rank": 2,
            "circulating_supply": 102421710,
            "total_supply": 102421710,
            "max_supply": null,
            "quotes": {
                "USD": {
                    "price": 223.378253997,
                    "volume_24h": 1539026622.1182,
                    "market_cap": 22878782821,
                    "percent_change_1h": -0.09,
                    "percent_change_24h": -1.13,
                    "percent_change_7d": -4.6
                }
            },
            "last_updated": 1538915312
        },
        "1": {
            "id": 1,
            "name": "Bitcoin",
            "symbol": "BTC",
            "website_slug": "bitcoin",
            "rank": 1,
            "circulating_supply": 17309562,
            "total_supply": 17309562,
            "max_supply": 21000000,
            "quotes": {
                "USD": {
                    "price": 6577.6247973,
                    "volume_24h": 3306420413.0069,
                    "market_cap": 113855804242,
                    "percent_change_1h": -0.5,
                    "percent_change_24h": -0.32,
                    "percent_change_7d": -0.5
                }
            },
            "last_updated": 1538915245
        }
    }
}

我暂时省略了元数据,因为不清楚您希望如何处理它。