Php 将格式化的数组写入文件
我想写这个数组Php 将格式化的数组写入文件,php,Php,我想写这个数组 $array = [ 'key1' => 'value1', 'key2' => [ 'subkey1' => 'value3' ], 'key3' => 'value4' ]; 到配置,它应该如下所示: config.php <?php return [ 'key1' => 'value1', 'key2' => [ 'subkey1' => '
$array = [
'key1' => 'value1',
'key2' => [
'subkey1' => 'value3'
],
'key3' => 'value4'
];
到配置,它应该如下所示:
config.php
<?php
return [
'key1' => 'value1',
'key2' => [
'subkey1' => 'value3'
],
'key3' => 'value4'
];
?>
用一个普通的PHP函数可以吗?我知道,使用var_export and print_r是不可能的。如果您在config.php中创建变量,那么当您在index.php中包含文件时,它将被加载,如下所示: config.php
做了一些假设,因为你的问题几乎没有什么信息,希望这有帮助 你的问题是?你能告诉我们你的需求是什么吗?用一个普通的PHP函数可以吗?不,不是速记版本,可能是
var\u export
但我更喜欢在这里使用serialize
。我可以通过打印“$var=unzerialize('.serialize($my_variable)。”)来创建文件代码>然后,我可以编码任何我想要的变量类型。
$config = include 'config.php';
<?php
$config = [
'key1' => 'value1',
'key2' => [
'subkey1' => 'value3'
],
'key3' => 'value4'
];
?>
include "config.php";
$value4 = $config['key3']; // 'value4'