我在php中使用file_put_contents()存储了txt文件中的数组,php数组在文本文件中写成功,同时如何将该文本文件读入php?
<?php
$arr = array('name','rollno','address');
file_put_contents('array.txt', print_r($arr, true));
?>
Run Code Online (Sandbox Code Playgroud)
上面的php写文本文件成功了.我想在php中读取该文本文件?
Kev*_*vin 12
如果您计划在数组中重用这些相同的值,则可以使用var_export创建该数组文件.
基本示例:
$arr = array('name','rollno','address');
file_put_contents('array.txt', '<?php return ' . var_export($arr, true) . ';');
Run Code Online (Sandbox Code Playgroud)
然后,当需要使用这些值时,只需使用include:
$my_arr = include 'array.txt';
echo $my_arr[0]; // name
Run Code Online (Sandbox Code Playgroud)
或者只使用一个简单的JSON字符串,然后编码/解码:
$arr = array('name','rollno','address');
file_put_contents('array.txt', json_encode($arr));
Run Code Online (Sandbox Code Playgroud)
然后,当你需要它时:
$my_arr = json_decode(file_get_contents('array.txt'), true);
echo $my_arr[1]; // rollno
Run Code Online (Sandbox Code Playgroud)