在php中如何正确地从两个单独的链接中读取两个数据点不同的JSON数组到一个数组中[duplicate]

nle07wnf  于 2023-02-03  发布在  PHP
关注(0)|答案(1)|浏览(114)
    • 此问题在此处已有答案**:

Merging arrays PHP(5个答案)
21小时前关门了。
此帖子在11小时前编辑并提交审查。
我的第一篇文章在这里的Stackoverflow.
在php中,如何正确地将两个不同数据点的JSON数组从两个独立的链接中读取到一个数组中?这里的目标是将两个独立的JSON数据集的值合并成一个数组,然后插入到MySql中。

Array 
(
    [1] => 'HourDK' value         // Dataset 1
    [2] => 'PriceArea' value      // Dataset 1
    [3] => 'SpotPriceDKK' value   // Dataset 1
    [4] => 'Price1' value         // Dataset 2
)

然后是24个数组,每小时一个,按"HourDK"排序。然后将这个数组插入MySQL。
链接到我的服务器:http://www.theos-kamp.dk/strom/json.php
链接到数据集1:https://api.energidataservice.dk/dataset/Elspotprices?start=2023-01-22T00%3A00&end=2023-01-23T00%3A00&columns=HourDK%2C%20PriceArea%2C%20SpotPriceDKK&filter=%7B%22PriceArea%22%3A%20%22DK2%22%7D
链接到数据集2:https://api.energidataservice.dk/dataset/DatahubPricelist?start=2023-01-22T00%3A00&end=2023-01-23T00%3A00&filter=%7B%22ChargeOwner%22%3A%20%22TREFOR%20El-net%20A%2FS%22%2C%20%22Note%22%3A%20%22Nettarif%20C%20time%22%7D&limit=1&timezone=DK
我的json.php文件

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>json</title>
</head>
<body>

    <?php
    // Nordpool pricelist
    $url1 = 'https://api.energidataservice.dk/dataset/Elspotprices?start=2023-01-22T00%3A00&end=2023-01-23T00%3A00&columns=HourDK%2C%20PriceArea%2C%20SpotPriceDKK&filter=%7B%22PriceArea%22%3A%20%22DK2%22%7D';
    // Grid pricelist
    $url2 = 'https://api.energidataservice.dk/dataset/DatahubPricelist?start=2023-01-22T00%3A00&end=2023-01-23T00%3A00&filter=%7B%22ChargeOwner%22%3A%20%22TREFOR%20El-net%20A%2FS%22%2C%20%22Note%22%3A%20%22Nettarif%20C%20time%22%7D&limit=1&timezone=DK';

    $json1 = file_get_contents($url1);
    $json2 = file_get_contents($url2);
    $json_array1 = json_decode($json1, true);
    $json_array2 = json_decode($json2, true);

    foreach($json_array1 as $key1 => $arrays1){
        
            foreach($arrays1 as $array1){
            echo "<br />";
            print_r(array_values($array1));

        }
            echo "<br />";
    }

    foreach($json_array2 as $key2 => $arrays2){
        
            foreach($arrays2 as $array2){
            echo "<br />";
            print_r(array_values($array2));

        }
            echo "<br />";
    }

    //$result = array_merge($array1, $array2);
    //print_r($result);
    
    //echo $array1, $array2;

    //foreach ($jo as $value) {
    //echo $jo->records;
    //}
    //echo $value, "\n";
    //echo var_dump($jo)
    //echo $jo->records;

    // Grid pricelist

    
    /*
    $url2 = 'https://api.energidataservice.dk/dataset/DatahubPricelist?start=2023-01-22T00%3A00&end=2023-01-23T00%3A00&filter=%7B%22ChargeOwner%22%3A%20%22TREFOR%20El-net%20A%2FS%22%2C%20%22Note%22%3A%20%22Nettarif%20C%20time%22%7D&limit=1&timezone=DK';
    $json = file_get_contents($url2);
    $json_array2 = json_decode($json, true);

    foreach($json_array2 as $key => $arrays){
        echo $key . "<br />";
        foreach($arrays as $array){
            echo "<br />";
            foreach($array as $key => $value){
                echo $key . " => " . $value . "<br />";
            }
            echo "<br />";
        }
        echo "<br />";
    }
    */

    /*
    $Price1 * 1.25 * 100 / 100
    $Price2
    $Price3
    $Price4
    $Price5
    $Price6
    $Price7
    $Price8
    $Price9
    $Price10
    $Price11
    $Price12
    $Price13
    $Price14
    $Price15
    $Price16
    $Price17
    $Price18
    $Price19
    $Price20
    $Price21
    $Price22
    $Price23
    $Price24
    */

    
    ?>

</body>
</html>
a8jjtwal

a8jjtwal1#

因为数据集2中的数据点不能使用foreach循环,你可以使用for循环,通过$var['Price' . $num]得到这些值。请看下面的例子,如果它适合你,请告诉我。

<?php
    $hour_prices = array();

    for ($hour = 0; $hour < 24; $hour++) {
        $hour_prices[] = array(
            $dataset_1['records'][$hour]['HourDK']
            , $dataset_1['records'][$hour]['PriceArea']
            , $dataset_1['records'][$hour]['SpotPriceDKK']
            , $dataset_2['records'][0]['Price' . ($hour + 1)]
        );
    }
?>

相关问题