将数组保存到文件txt

时间:2020-01-17 20:44:22

标签: php arrays file save

我无法将表格数据正确保存到文件中。

  1. 从文件将数据加载到数组中
  2. 我检查从字段中输入的代码是否在表格中
  3. 如果是的话,我会在表格中更改此条目
  4. 我以表格形式将表格写入txt文件 1号 2号 数字3

但是有问题

我的代码

$find = $_POST['pole_1'];

$array = [];
$array = file('voucher.txt', FILE_IGNORE_NEW_LINES);

// check array after read from file
print_r($array);

echo '<br />';echo '<br />';

// I check if it is in the array and if so I change the number
for( $i = 0; $i < count($array); $i++ ) {  

    if ($find === $array[$i]) {
        echo "Match found"; echo '<br />';
        $array[$i] = $array[$i] . '_code_use';   
    } else if ($find == ''){
        echo "Empty field"; echo '<br />';

    } else {
        echo "Match NOT found"; echo '<br />';

    }
}

// check array after changge
echo '<br />';
print_r($array);

// Saving to file
$file = 'new.txt';
for( $i = 0; $i < count($array); $i++ ) {  
    $data = $array[$i]."\n";
    file_put_contents($file, $data);

}

文件Voucher.txt。 ->数组

01238
08752
09765

$ find ='08752'

循环后的数组,我希望将此保存到文件

01238
08752_code_use
09765

0 个答案:

没有答案
相关问题