Php : 将文件内容分成不同的文件

Php : Separate file content into different files

我有这个文件

1 + 3 = 4
0 + 0 = 0
1 + 2 = 3
1 / 1 = 1
2 * 3 = 6

我想将 (firstNum, secondNum, operationUsed(+,-,*,/), and result) 分成 4 个不同的文件

不知道为什么你会需要这个,但是给你

<?php
// read
$array = file('original.txt');

// parse
$set = [
 'firstNum' => [],
 'secondNum' => [],
 'operationUsed' => [],
 'result' => [],
];
foreach ($array as $value) {
    $value = explode(' ', $value);
    
    $set['firstNum'][] = $value[0];
    $set['secondNum'][] = $value[1];
    $set['operationUsed'][] = $value[2];
    $set['result'][] = $value[3];
}

// write
file_put_contents('firstNum.txt', implode(PHP_EOL, $set['firstNum']));
file_put_contents('secondNum.txt', implode(PHP_EOL, $set['secondNum']));
file_put_contents('operationUsed.txt', implode(PHP_EOL, $set['operationUsed']));
file_put_contents('result.txt', implode(PHP_EOL, $set['result']));