LoginSignup
0
0

More than 1 year has passed since last update.

for文使って配列をファイルに保存

Posted at

概要

上記事では複数のテキストフォームの内容をファイルに保存した。
今回は配列データをfor文使ってファイルに保存する。

<?php
    //ファイルに書き込むデータ作成
    $string = array("test0","test1","test2","test3","test4","test5","test6","test7","test8","test9");

    $file_name="data.txt"; //ファイル名
    $file = fopen($file_name,"w");  //ファイルオープン
    flock($file, LOCK_EX);  //ファイルロック

    //データを書き込む
    for($i=0; $i<10; $i++){
      fputs($file, $string[$i]);
      fputs($file, "\n");
    }

    flock($file, LOCK_UN);  //ファイルロック解除
    fclose($file);  //ファイルクローズ
?>

参考文献

・PHPによるWebアプリケーションスーパーサンプル第2版
・速攻!図解プログラミングPHP+MySQL

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0