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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?