简体   繁体   English

在PHP文件中编辑数组

[英]Edit array in PHP file

I have a small issue. 我有一个小问题。 Is there (in PHP) any way to update array in the php file ? 有(在PHP中)有什么方法可以更新php文件中的数组吗? For example I have file with this code 例如我有此代码的文件

$array = array(
    'test' => 'aaaa'
    'test2' => 'aaaa'
    'test3' => 'aaaa'
     etc etc, more data
);

I load this array via include file.php using $array, I do some stuff and pick just few keys I need to delete (they are unused, ...). 我使用$ array通过include file.php加载了这个数组,我做了一些事情,只选择了一些我需要删除的键(它们是未使用的,...)。 Is there any way how to do it nicely? 有什么方法可以很好地做到这一点吗? I mean I could delete file and fwrite first line, then use foreach for every key and at the end write ); 我的意思是我可以删除文件并fwrite第一行,然后对每个键使用foreach,最后写入); to file. 归档。 But there is a problem. 但有一个问题。 The file does not include just this array (There are multiple variables, texts,...). 该文件不只包含此数组(有多个变量,文本等)。

So could anyone help me? 有人可以帮我吗? Please dont ask me why its done this way, Im not happy about it either, I just need to solve this. 请不要问我为什么这样做,我也不满意,我只需要解决这个问题。

Summary: I have the array keys i want to delete in array + I have array in .php file I need to modify nicely 摘要: 我有要在数组中删除的数组键+我在.php文件中有数组,我需要很好地进行修改

If you really want to store your data as php array , instead of some database then you should use: serialize() and unserialize() 如果您确实想将数据存储为php array而不是某些数据库,则应使用: serialize()unserialize()

Then your workflow will look like: 然后您的工作流程将如下所示:

  1. Save your data as serialized array to a .php file. 将数据作为序列化数组保存到.php文件。
  2. Load it from there and unserialize . 从那里加载它并反序列化
  3. Work on it. 在...上下功夫。
  4. If your work with the array is ended, serialize and save to the file again. 如果使用阵列的操作结束,请序列化并再次保存到文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM