php - 使用 file_put_contents 的数组

标签 php arrays

假设我有 2 个数组。

$arr1=array("foo"=>"bar", 1=>"one", 2=>"two");
$arr2=array("h"=>"eich", 3=>"three", 4=>"four");

通过使用 file_put_contents 我可以将数组打印到一个新的 php 文件中,如下所示:

<?php
$arr1 = Array
(
   "foo"=>"bar",
    1=>"one",
    2=>"two"
) //need a semicolon here 
$arr2 = Array
(
   "h"=>"eich",
    3=>"three",
    4=>"four"
)//need a semicolon here 

我的问题是,如何在每个数组末尾后得到一个分号?

最佳答案

因为你甚至有这个问题,我猜你正在循环数组以将它们转换为 PHP 代码,而不是使用 var_export() - 你应该是:

$arr1 = array("foo"=>"bar", 1=>"one", 2=>"two");
$arr2 = array("h"=>"eich", 3=>"three", 4=>"four");

$file = "<?php\n\n".var_export($arr1, TRUE).";\n\n".var_export($arr2, TRUE).";\n";

file_put_contents('newfile.php', $file);

See it working

关于php - 使用 file_put_contents 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11540062/

相关文章:

arrays - perl 打开文件名中带有变量的文件

php - Zend Framework 2 应用程序中 composer.phar self 更新的 Composer\Downloader\TransportException

php - 在 PHP 5.4 下运行 PHPMyAdmin

c# - 如何对每个输入将数组加 1

c - 在C中解析 "String"(字符数组)为字符串,int和int

c - 如何初始化一个只有 NULL 指针的字符串数组?

php - 在 cakephp3.0 内部错误的情况下强制用户注销

php - 在内部页面上索引和搜索文本的最佳解决方案?

php - 使用 PHP 将 DateTime 插入 SQL Server

arrays - 删除 Swift 数组中所有出现的值