使用 fwrite() 在具有内部数组的 php 文件中添加变量?
Add variable in php file with inside array using fwrite()?
我在 php 文件中使用 fwrite
添加变量时遇到问题。
我的php文件有一个数组,是这样的:
<?php
$array48= array (
0 =>
array (
0 => '2019-04-24-1248326-1313',
1 => 1556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
我想在文件开头添加变量:
$fp_due_min_fa = fopen('../file.php', 'r+');
fwrite($fp_due_min_fa, '<?php ' . "\n" . '$id = ' . $id_terr . ';' . "\n");
fclose($fp_due_min_fa);
但是在这段代码之后 php 文件是这样的:
<?php
$id = 123456;
556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
数组被破坏...
我想要这样的输出:
<?php
$id = 123456;
$array48= array (
0 =>
array (
0 => '2019-04-24-1248326-1313',
1 => 1556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
为什么我会出现这个错误?
整体设计看起来很糟糕,但一般来说只是读取、连接和写入以前置到开头。为了不必去掉开头的 PHP 标签,只需打开和关闭标签即可:
$output = "<?php $id = $id_terr; ?>\n";
$file = file_get_contents('../file.php');
file_put_contents('../file.php', $output.$file);
或追加(假设没有关闭 PHP 标记,如图所示),只需:
file_put_contents('../file.php', "\n$id = $id_terr;\n", FILE_APPEND);
我在 php 文件中使用 fwrite
添加变量时遇到问题。
我的php文件有一个数组,是这样的:
<?php
$array48= array (
0 =>
array (
0 => '2019-04-24-1248326-1313',
1 => 1556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
我想在文件开头添加变量:
$fp_due_min_fa = fopen('../file.php', 'r+');
fwrite($fp_due_min_fa, '<?php ' . "\n" . '$id = ' . $id_terr . ';' . "\n");
fclose($fp_due_min_fa);
但是在这段代码之后 php 文件是这样的:
<?php
$id = 123456;
556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
数组被破坏...
我想要这样的输出:
<?php
$id = 123456;
$array48= array (
0 =>
array (
0 => '2019-04-24-1248326-1313',
1 => 1556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
为什么我会出现这个错误?
整体设计看起来很糟糕,但一般来说只是读取、连接和写入以前置到开头。为了不必去掉开头的 PHP 标签,只需打开和关闭标签即可:
$output = "<?php $id = $id_terr; ?>\n";
$file = file_get_contents('../file.php');
file_put_contents('../file.php', $output.$file);
或追加(假设没有关闭 PHP 标记,如图所示),只需:
file_put_contents('../file.php', "\n$id = $id_terr;\n", FILE_APPEND);