duanhe4155 2015-06-04 19:03
浏览 33
已采纳

如何在PHP中将结构写入文件?

I have learned that to create a structure, I can create an array like so:

$MyStructure = array (
    'id' => '12345',
    'name'  => 'myName');

I also know, to write into a file, I can use:

file_put_contents($fileName, $myTextToSave, FILE_APPEND);

But, this function stores it in simple text format. I want to know about functions such as in C-Language: fwrite( &record, sizeof(struct myStructure), 1, fp );

Is there any function in PHP which can store data in chunks of data-structure and then retrieve it in data structure format?

  • 写回答

2条回答 默认 最新

  • dqba94394 2015-06-04 19:10
    关注

    You could write your structure to a file with file_put_contents as you have outlined in your post.

    You can serialize and unserialize the arrays or objects into the file using the following documentation:

    http://php.net/manual/en/function.serialize.php

    http://php.net/manual/en/function.unserialize.php

    So it would look like this:

    file_put_contents($fileName, serialize($MyStructure), FILE_APPEND);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计