将数组写入文件的最佳方法? [英] Best way to write arrays to a file?

查看:70
本文介绍了将数组写入文件的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想避免写入数据库,而对lang文件等使用常量/数组.

I want to avoid writing to DB and use constants/array for lang files etc.

即:

$lang = array (
  'hello' => 'hello world!'
);

并可以在后台进行编辑. (然后,我将只使用$ lang ['hello'].而不是从可怜的数据库中获取它.)

and be able to edit it from the back office. (then instead of fetching it from the poor db, i would just use $lang['hello']..).

您提出什么建议以最好,最有效的方式提取它?

what are you suggesting for the best and efficient way to pull it of?

推荐答案

绝对是JSON

要保存:

file_put_contents("my_array.json", json_encode($array));

要找回它:

$array = json_decode(file_get_contents("my_array.json"));

就这么简单!

这篇关于将数组写入文件的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆