使用fwrite()在带有内部数组的php文件中添加变量?

我在使用数组内部数组在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,

   )

 )

为什么我有这个错误?


眼眸繁星
浏览 125回答 1
1回答

MYYA

总体设计看起来很糟糕,但总体而言,它只是读,连接和写入,所以要一直挂在开头。为了不必去除开头的PHP标记,只需打开和关闭标记:$output = "<?php \$id = $id_terr; ?>\n";$file&nbsp; &nbsp;= 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);
打开App,查看更多内容
随时随地看视频慕课网APP