php - 如何使用脚本硬编码附加到 PHP 文件上的数组?
问题描述
例如,一个 PHP 文件包含以下内容:
<?php
return [
'sample_array' => [
'sample_index' => 'sample_data',
],
];
我想将值附加到sample_array
使用 PHP 脚本。最好的方法是什么?
编辑:运行脚本后,文件应更新为:
<?php
return [
'sample_array' => [
'sample_index' => 'sample_data',
'sample_index2' => 'sample_data2',
],
];
编辑:我正在寻找的是创建一个脚本,它将直接更新 PHP 文件的内容以附加数组。
解决方案
您可以通过使用var_export()
. 然后,您可以将代码存储在具有类似file_put_contents()
.
// load the array from the file
$arr = include 'file.php';
// modify the array
$arr['sample_array']['sample_index2'] = 'sample data 2';
// write it back to the file
file_put_contents("file.php", "return " . var_export($arr, true) . ";");
给你...
返回数组 ( 'sample_array' => 大批 ( 'sample_index' => 'sample_data', 'sample_index2' => '样本数据 2', ), );
推荐阅读
- python - Python:如何选择包含一年中特定月份的列?
- wordpress - 如何从小部件区域获取/显示所有小部件?
- javascript - iOS 和 Safari 中的网站滚动到顶部控制/载体栏 - 如何停止?
- azure - 使用 Azure 数据流缓存接收器值动态命名输出文件
- intellij-idea - java.io.IOException:无法运行程序“connect.exe”:CreateProcess 错误=2
- reactjs - 在 laravel 8 中安装 react 时有 91 个软件包正在寻找资金
- html - 如何在固定位置元素中居中元素
- java - 使用 OffSet 处理字符串日期
- excel - 触控板滚动时出现 Excel 冻结窗格问题
- c# - 使用 TFS 2018 与 VS 2019 构建解决方案