PHP将字符串数据写入arra

问题描述 投票:-1回答:1

我在过去一个小时内得到温度读数。数据以一行的形式出现,每10分钟显示一次数据。

200504, 0530, 0, 262.3, 1.399, 6.097, 2.15, 
200504, 0540, 0, 251.2, 1.29, 6.08, 2.09,
200504, 0550, 0, 249.4, 1.685, 5.921, 2.44,
200504, 0600, 0, 249.5, 1.465, 5.904, 2.27,
200504, 0610, 0, 247.7, 1.801, 6.214, 2.61,
200504, 0620, 0, 246.9, 1.908, 6.532, 3.04,

需要计算该行最后一个元素的平均值

$str = '190227, 2020, 9, 245.8, 2.886, 5.753, 0';

$pattern = '/([\S]*),\s([\S]*),\s([\S]*),\s([\S]*),\s([\S]*),\s([\S]*),\s([\S]*)/m';

preg_match($pattern, $str, $matches);

$result =  [
    "Date" =>  intval($matches[1]),
    "Time" =>  intval($matches[2]),
    "h" => intval($matches[3]),
    "Deg.M" => floatval($matches[4]),
    "m/s" => floatval($matches[5]),
    "deg.C" => floatval($matches[6]),
    "mm/h" => floatval($matches[7])
];

通过这种解决方案,我可以获取一个时间间隔的数据,但是如何将所有这些数据写入一个数组并计算平均值?

php arrays php-7
1个回答
1
投票

这是您的问题的答案,包括解决方案:

[如果您有一个文件,例如data.txt并且该文件包含:

data.txt:

Line 1: 200504, 0530, 0, 262.3, 1.399, 6.097, 2.15, 
Line 2: 200504, 0540, 0, 251.2, 1.29, 6.08, 2.09,
Line 3: 200504, 0550, 0, 249.4, 1.685, 5.921, 2.44,
Line 4: 200504, 0600, 0, 249.5, 1.465, 5.904, 2.27,
Line 5: 200504, 0610, 0, 247.7, 1.801, 6.214, 2.61,
Line 6: 200504, 0620, 0, 246.9, 1.908, 6.532, 3.04,

将文件读入行:

使用file('path/to/filename.txt');将文件读入数组的每一行一个元素:

$filecontent = file('test.txt');
print_r($filecontent);

/*
    output:
    Array
    (
        [0] => 200504, 0530, 0, 262.3, 1.399, 6.097, 2.15,

        [1] => 200504, 0540, 0, 251.2, 1.29, 6.08, 2.09,

        [2] => 200504, 0550, 0, 249.4, 1.685, 5.921, 2.44,

        [3] => 200504, 0600, 0, 249.5, 1.465, 5.904, 2.27,

        [4] => 200504, 0610, 0, 247.7, 1.801, 6.214, 2.61,

        [5] => 200504, 0620, 0, 246.9, 1.908, 6.532, 3.04,

    )
*/

这是您要求的代码:

根据我的喜好,我更喜欢不带/,.字符的小写数组键:

<?php

$filecontent = file('test.txt');

// this is the array for results
$results = array();

foreach($filecontent as $line) {
    // remove the \r\n new line character
    $line = trim($line);

    // remove the last comma if exists
    $line = rtrim($line, ',');

    // split to parts
    $parts = explode(', ', $line);

    // add to result array
    $results[] = array(
        'Date'  => $parts[0],
        'Time'  => $parts[1],
        'h'     => $parts[2],
        'Deg.M' => $parts[3],
        'm/s'   => $parts[4],
        'deg.C' => $parts[5],
        'mm/h'  => $parts[6],
    );
}


print_r($results);
/*
output:
Array
(
    [0] => Array
        (
            [Date] => 200504
            [Time] => 0530
            [h] => 0
            [Deg.M] => 262.3
            [m/s] => 1.399
            [deg.C] => 6.097
            [mm/h] => 2.15
        )

    [1] => Array
        (
            [Date] => 200504
            [Time] => 0540
            [h] => 0
            [Deg.M] => 251.2
            [m/s] => 1.29
            [deg.C] => 6.08
            [mm/h] => 2.09
        )

    [2] => Array
        (
            [Date] => 200504
            [Time] => 0550
            [h] => 0
            [Deg.M] => 249.4
            [m/s] => 1.685
            [deg.C] => 5.921
            [mm/h] => 2.44
        )

    [3] => Array
        (
            [Date] => 200504
            [Time] => 0600
            [h] => 0
            [Deg.M] => 249.5
            [m/s] => 1.465
            [deg.C] => 5.904
            [mm/h] => 2.27
        )

    [4] => Array
        (
            [Date] => 200504
            [Time] => 0610
            [h] => 0
            [Deg.M] => 247.7
            [m/s] => 1.801
            [deg.C] => 6.214
            [mm/h] => 2.61
        )

    [5] => Array
        (
            [Date] => 200504
            [Time] => 0620
            [h] => 0
            [Deg.M] => 246.9
            [m/s] => 1.908
            [deg.C] => 6.532
            [mm/h] => 3.04
        )

)
*/
© www.soinside.com 2019 - 2024. All rights reserved.