Home > Backend Development > PHP Tutorial > PHP writes array to file example code_PHP tutorial

PHP writes array to file example code_PHP tutorial

WBOY
Release: 2016-07-13 10:44:57
Original
859 people have browsed it

There are many ways to write arrays into files in PHP. For example, if we want to make a cache file, we will convert the PHP array into a file and save it to a .php file. When calling, we can directly call this file.

php writes the array to the file through sequence and desequence. Please see the code


$file="./data/file.cache";
file_put_contents($file,serialize($array));//Write cache

The code is as follows Copy code
 代码如下 复制代码

$file="./data/file.cache";

$array = array("count" => "3000",
 
               "num"  =>"300");
 
//缓存
 
file_put_contents($file,serialize($array));//写入缓存
?>

$file="./data/file.cache";
$handle = fopen($file, "r");
$cacheArray = unserialize(fread($handle, filesize ($file)));
print_r($cacheArray);
?>

$file="./data/file.cache";

代码如下 复制代码

$write_array = array( '1' => 'oneone', '2' => 'two', '3' => 'three', '4' => 'four', '5' => 'five' ); //字符串处理
$string_start = ""; $string = $string_start.$string_process.$string_end; //开始写入文件
echo file_put_contents('test_array.php', $string);
?>

$array = array("count" => "3000",


"num" =>"300");

 代码如下 复制代码

1,var_export():

//Cache

file_put_contents($file,serialize($array));//Write cache
 代码如下 复制代码
array ( 1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', )
?>


$file="./data/file.cache";

$handle = fopen($file, "r");

$cacheArray = unserialize(fread($handle, filesize ($file)));
代码如下 复制代码
string(86) "array ( 1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', )"
print_r($cacheArray); ?> //Write a test array into a PHP file:

The code is as follows Copy code
<🎜>$write_array = array( '1' => 'oneone', '2' => 'two', '3' => 'three', '4' => 'four', '5 ' => 'five' ); //String processing $string_start = ""; $string = $string_start.$string_process.$string_end; //Start writing file echo file_put_contents('test_array.php', $string); ?>
Two functions are used here:
The code is as follows Copy code
1, var_export():
·var_export — used to output or return a string representation of a variable. The difference between it and var_dump() is that var_export() can be used to return structural information about the variable passed to the function, and the representation it returns It is legal PHP code. If "echo $string_process;", you can see the output:
The code is as follows Copy code
array ( 1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', )
And this is what we want to write to the test_array.php file (minus the php tag); ·The var_dump() function is used to print information about variables. It is only used to "print" and does not return a value. Its prototype is void var_dump(...), let's "var_dump($string_process); ”, you can see the output result:
The code is as follows Copy code
string(86) "array ( 1 => ; 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', )"

You can see the output string(86) "...", which once again shows that var_export() returns a string.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633068.htmlTechArticleThere are many ways to write arrays to files in php. For example, if we want to make a cache file, we will use php The array is converted into a file and then saved to a .php file. This file can be called directly when calling. p...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template