重构文本以实现一系列的超时和超时

时间:2019-02-22 05:45:05

标签: php

这是我从生物识别技术中导出的数据,并且以某种方式,其格式为.txt:

UDISKLOG    version=2   date=2019-02-21 firmware=FK254HS30_en_v132
No  Mchn    EnNo        Name        Mode    IOMd    DateTime    
000001  1   000000001   ting            268435456   2305    2019/02/16  10:15:56
000002  1   000000001   ting            268435456   2305    2019/02/16  13:45:58
000003  1   000000001   ting            268435456   2305    2019/02/16  13:46:04
000004  1   001500022   julie           268435456   2305    2019/02/16  13:48:52
000005  1   000000001   ting            268435456   2305    2019/02/21  17:33:16
000006  1   000000001   ting            268435456   2305    2019/02/21  18:14:15
000007  1   001500022   julie           268435456   2305    2019/02/21  18:14:55
000008  1   000000002   marielle        268435456   2305    2019/02/21  18:18:15
000009  1   000000001   ting            268435456   2305    2019/02/21  18:52:54
000010  1   000000002   marielle        268435456   2305    2019/02/21  18:53:31
000011  1   000000002   marielle        268435456   2305    2019/02/21  18:55:57
000012  1   000000002   marielle        268435456   2305    2019/02/21  18:56:07
000013  1   001500022   julie           268435456   2305    2019/02/21  20:42:36
000014  1   000000001   ting            268435456   2305    2019/02/21  21:00:23
000015  1   000000001   ting            268435456   2305    2019/02/21  21:02:21
000016  1   000000001   ting            268435456   2305    2019/02/21  21:11:09

以某种方式,我设法通过此块代码将其转换为仅包含重要细节的数组:

$file = file('GLG_001.txt', FILE_IGNORE_NEW_LINES);
$data = [];

unset($file[0]);
unset($file[1]);

foreach($file as $files){
    $explode = explode(' ', $files);

    $first_explode = preg_split('/\s+/', $explode[0]);

    $data['student_id'][] = $first_explode[2];
    $data['time'][] = end($explode);
    $explode1 = $explode[count($explode) - 3];
    $second_explode = preg_split('/\s+/', $explode1);
    $data['date'][] = $second_explode[count($second_explode) - 1];
}

print_r($data);

随后导致的结果:

Array
(
[student_id] => Array
    (
        [0] => 000000001
        [1] => 000000001
        [2] => 001500022
        [3] => 000000002
        [4] => 000000001
        [5] => 000000002
        [6] => 000000002
        [7] => 000000002
        [8] => 001500022
        [9] => 000000001
        [10] => 000000001
        [11] => 000000001
    )

[time] => Array
    (
        [0] => 17:33:16
        [1] => 18:14:15
        [2] => 18:14:55
        [3] => 18:18:15
        [4] => 18:52:54
        [5] => 18:53:31
        [6] => 18:55:57
        [7] => 18:56:07
        [8] => 20:42:36
        [9] => 21:00:23
        [10] => 21:02:21
        [11] => 21:11:09
    )

[date] => Array
    (
        [0] => 2019/02/21
        [1] => 2019/02/21
        [2] => 2019/02/21
        [3] => 2019/02/21
        [4] => 2019/02/21
        [5] => 2019/02/21
        [6] => 2019/02/21
        [7] => 2019/02/21
        [8] => 2019/02/21
        [9] => 2019/02/21
        [10] => 2019/02/21
        [11] => 2019/02/21
    )

)

请注意,数组中存在相同的学生ID,即学生输入其生物特征信息到系统中的时间。第一个唯一ID表示他/她进入,而最后一个ID表示超时。在给定的数组中,学生ID 000000001在键[0]中,而他的最后一个ID在键[11]中。将其与$data['time']进行比较,他/她的停留时间为[0] => 17:33:16,他/她的停留时间为[11] => 21:11:09。确定超时和超时后,我要将其存储到数组中。因此,$data['student_id']随后将通过array_unique()转换为具有唯一值的数组,因此,存储时间和超时也将存储到与count(array_uniqe($data['student_id']))相同的数组中。所以最终输出将是:

Array(
[student_id] => Array
    (
        [0] => 000000001
        [1] => 001500022
        [2] => 000000002
    )
[time_in] => Array
    (
        [0] => 17:33:16
        [1] => 18:14:55
        [2] => 18:18:15
    )

[time_out] => Array
    (
        [0] => 21:11:09
        [1] => 20:42:36
        [2] => 18:56:07
    )
[date] => Array
    (
        [0] => 2019/02/21
        [1] => 2019/02/21
        [2] => 2019/02/21
    )
)

但是,我不知道最后一部分该怎么做,要花费时间和超时。

5 个答案:

答案 0 :(得分:0)

$student_id = array("000000001",
     "000000001",
     "001500022",
     "000000002",
     "000000001",
     "000000002",
     "000000002",
     "000000002",
     "001500022",
     "000000001",
     "000000001",
     "000000001"
    );

$time = array("17:33:16",
        "18:14:15",
        "18:14:55",
        "18:18:15",
        "18:52:54",
        "18:53:31",
        "18:55:57",
        "18:56:07",
        "20:42:36",
        "21:00:23",
        "21:02:21",
        "21:11:09"
    );

$date= array("2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21",
        "2019/02/21"
    );


    $newArr = array_unique($student_id);
    $newRevArr = array_reverse($newArr);
    $time_reverse = array_reverse($time);
    $date_reveres = array_reverse($date);

    foreach($newArr as $key=>$value) {
        echo $value ." signed in: ". $time[$key]." signed out: ".     $time_reverse[$key] ."<br .>\n";
    }

结果:

000000001 signed in: 17:33:16 signed out: 21:11:09
001500022 signed in: 18:14:55 signed out: 21:00:23
000000002 signed in: 18:18:15 signed out: 20:42:36

答案 1 :(得分:0)

您可以使用此循环基于学生ID和日期创建多维关联数组。
代码创建具有结构$arr[ID][date][time]

的数组
$arr = array_slice(explode("\n", $data),2);
foreach($arr as $line){
    $temp = preg_split("/\s+/", $line);
    $result[$temp[2]][$temp[6]][] = end($temp);
}

结果是:

array(3) {
  ["000000001"]=>
  array(2) {
    ["2019/02/16"]=>
    array(3) {
      [0]=>
      string(8) "10:15:56"
      [1]=>
      string(8) "13:45:58"
      [2]=>
      string(8) "13:46:04"
    }
    ["2019/02/21"]=>
    array(6) {
      [0]=>
      string(8) "17:33:16"
      [1]=>
      string(8) "18:14:15"
      [2]=>
      string(8) "18:52:54"
      [3]=>
      string(8) "21:00:23"
      [4]=>
      string(8) "21:02:21"
      [5]=>
      string(8) "21:11:09"
    }
  }
  ["001500022"]=>
  array(2) {
    ["2019/02/16"]=>
    array(1) {
      [0]=>
      string(8) "13:48:52"
    }
    ["2019/02/21"]=>
    array(2) {
      [0]=>
      string(8) "18:14:55"
      [1]=>
      string(8) "20:42:36"
    }
  }
  ["000000002"]=>
  array(1) {
    ["2019/02/21"]=>
    array(4) {
      [0]=>
      string(8) "18:18:15"
      [1]=>
      string(8) "18:53:31"
      [2]=>
      string(8) "18:55:57"
      [3]=>
      string(8) "18:56:07"
    }
  }
}

https://3v4l.org/uX0ig
您也可以像这样将名称添加到数组中:
https://3v4l.org/BbWid


如果只想输出输入和输出时间,则可以如下循环结果数组:

foreach($result as $student => $subarray){
    echo $student . " " . $subarray['name'] . PHP_EOL;
    unset($subarray['name']);
    foreach($subarray as $date => $val){
        echo 'date ' . $date . ' sign in ' . $val[0] . ", sign out " . end($val) . PHP_EOL;
    }
    echo PHP_EOL . PHP_EOL;
}

结果是:

000000001 ting
date 2019/02/16 sign in 10:15:56, sign out 13:46:04
date 2019/02/21 sign in 17:33:16, sign out 21:11:09


001500022 julie
date 2019/02/16 sign in 13:48:52, sign out 13:48:52
date 2019/02/21 sign in 18:14:55, sign out 20:42:36


000000002 marielle
date 2019/02/21 sign in 18:18:15, sign out 18:56:07

答案 2 :(得分:0)

从您的问题中,我了解到您想获得学生的“第一时间”和“最后时间”。

我认为将日期和时间分别放在不同的数组中会增加难度。因此,在将文件读入数组时,最好将时间和日期放在一起。 稍后,您可以轻松地分隔日期和时间。

如果我正确地理解了您的问题,看来您只需要找到每个学生的第一时间和最后时间的数组的最小和最大索引即可。

我在下面写一些代码和结果。希望对您有帮助。

//请注意,我将日期和时间放在一起。

<?php

// Array given 
$ar_data['student_id'][0] = '000000001';
$ar_data['student_id'][1] = '000000001';
$ar_data['student_id'][2] = '001500022';
$ar_data['student_id'][3] = '000000002';
$ar_data['student_id'][4] = '000000001';
$ar_data['student_id'][5] = '000000002';
$ar_data['student_id'][6] = '000000002';
$ar_data['student_id'][7] = '000000002';
$ar_data['student_id'][8] = '001500022';
$ar_data['student_id'][9] = '000000001';
$ar_data['student_id'][10] = '000000001';
$ar_data['student_id'][11] = '000000001';

$ar_data['time'][0] = '2019/02/21 17:33:16';
$ar_data['time'][1] = '2019/02/21 18:14:15';
$ar_data['time'][2] = '2019/02/21 18:14:55';
$ar_data['time'][3] = '2019/02/21 18:18:15';
$ar_data['time'][4] = '2019/02/21 18:52:54';
$ar_data['time'][5] = '2019/02/21 18:53:31';
$ar_data['time'][6] = '2019/02/21 18:55:57';
$ar_data['time'][7] = '2019/02/21 18:56:07';
$ar_data['time'][8] = '2019/02/21 20:42:36';
$ar_data['time'][9] = '2019/02/21 21:00:23';
$ar_data['time'][10] = '2019/02/21 21:02:21';
$ar_data['time'][11] = '2019/02/21 21:11:09';


// Restructure
// Get all in and out time 
foreach ($ar_data['student_id'] as $index => $value) {
    $ar_new[$value][] = $ar_data['time'][$index];
}

// Make a result array
foreach ($ar_new as $student_id => $ar_time_list) {
    $ar_result['student_id'][] = $student_id;
    $ar_result['time_in'][] = $ar_time_list[0];
    $count_time = count($ar_time_list) - 1;
    $ar_result['time_out'][] = $ar_time_list[$count_time];
}


echo "\nResult:\n";
print_r($ar_result);

?>

----------------------------------------------- -------------------------------------

Result:
Array
(
    [student_id] => Array
        (
            [0] => 000000001
            [1] => 001500022
            [2] => 000000002
        )

    [time_in] => Array
        (
            [0] => 2019/02/21 17:33:16
            [1] => 2019/02/21 18:14:55
            [2] => 2019/02/21 18:18:15
        )

    [time_out] => Array
        (
            [0] => 2019/02/21 21:11:09
            [1] => 2019/02/21 20:42:36
            [2] => 2019/02/21 18:56:07
        )

)

答案 3 :(得分:0)

几乎完成尝试...

$data = Array
(
"student_id" => Array
    (
        "0" => "000000001",
        "1" => "000000001",
        "2" => "001500022",
        "3" => "000000002",
        "4" => "000000001",
        "5" => "000000002",
        "6" => "000000002",
        "7" => "000000002",
        "8" => "001500022",
        "9" => "000000001",
        "10" => "000000001",
        "11" => "000000001"
    ),

"time" => Array
    (
        "0" => "17:33:16",
        "1" => "18:14:15",
        "2" => "18:14:55",
        "3" => "18:18:15",
        "4" => "18:52:54",
        "5" => "18:53:31",
        "6" => "18:55:57",
        "7" => "18:56:07",
        "8" => "20:42:36",
        "9" => "21:00:23",
        "10" => "21:02:21",
        "11" => "21:11:09"
    ),

"date" => Array
    (
        "0" => "2019/02/21",
        "1" => "2019/02/21",
        "2" => "2019/02/21",
        "3" => "2019/02/21",
        "4" => "2019/02/21",
        "5" => "2019/02/21",
        "6" => "2019/02/21",
        "7" => "2019/02/21",
        "8" => "2019/02/21",
        "9" => "2019/02/21",
        "10" => "2019/02/21",
        "11" => "2019/02/21"
    )

);

//echo "<pre>";print_r($data);

$data["time_in"] = [];
$data["time_out"] = [];
for ($i = 0; $i < count($data["student_id"]); $i++) {
    for ($j = (count($data["student_id"]) - 1); $j < count($data["student_id"]); $j--) {
        if ($data["student_id"][$i] == $data["student_id"][$j]) {
            array_push($data["time_in"], $data["time"][$i]);
            array_push($data["time_out"], $data["time"][$j]);
            unset($data["student_id"][$j]);
            unset($data["time"][$i]);
            unset($data["time"][$j]);
            unset($data["date"][$j]);
            break;
        }
    }
}

echo "<pre>";print_R($data);

答案 4 :(得分:0)

这是完整的工作代码。最好的做法是将数据存储在多维数组中。

<?php
  $finalData = [];
  $file = array_splice(file('GLG_001.txt', FILE_IGNORE_NEW_LINES), 2);

  foreach($file as $files){
    $explodedData = preg_split('/\s+/', $files);
    $curTimestamp = strtotime($explodedData[6].' '.$explodedData[7]);
    $finalData[$explodedData[2]][$explodedData[6]][] = $curTimestamp;
  }
  foreach ($finalData as $studentId => $studentData) {
    foreach ($studentData as $date => $times) {
      sort($times);
      unset($finalData[$studentId][$date]);
      $finalData[$studentId][$date]['timeIn'] = date('H:i:s', $times[0]);
      $finalData[$studentId][$date]['timeOut'] = date('H:i:s', end($times));
    }
  }
  echo '<pre>'; print_r($finalData); echo '</pre>';
?>

输出:

Array
(
    [000000001] => Array
        (
            [2019/02/16] => Array
                (
                    [timeIn] => 10:15:56
                    [timeOut] => 13:46:04
                )

            [2019/02/21] => Array
                (
                    [timeIn] => 17:33:16
                    [timeOut] => 21:11:09
                )

        )

    [001500022] => Array
        (
            [2019/02/16] => Array
                (
                    [timeIn] => 13:48:52
                    [timeOut] => 13:48:52
                )

            [2019/02/21] => Array
                (
                    [timeIn] => 18:14:55
                    [timeOut] => 20:42:36
                )

        )

    [000000002] => Array
        (
            [2019/02/21] => Array
                (
                    [timeIn] => 18:18:15
                    [timeOut] => 18:56:07
                )

        )

)