Home > php教程 > php手册 > PHP: 把数组存入文件

PHP: 把数组存入文件

WBOY
Release: 2016-06-06 19:50:59
Original
767 people have browsed it

有时候会动态更新配置文件,很多配置文件的内容格式为数组。 那么就需要把一个数组存入配置文件。方法很简单: 使用print_r或var_export就可以,该函数默认把数据打印到屏幕,但如果第二个参数为true,则直接返回数据。 $b = array ( 'm' = 'monkey' , 'foo'

有时候会动态更新配置文件,很多配置文件的内容格式为数组。

那么就需要把一个数组存入配置文件。方法很简单:

使用print_r或var_export就可以,该函数默认把数据打印到屏幕,但如果第二个参数为true,则直接返回数据。


<code><span>$b </span><span>=</span><span> array </span><span>(</span><span>'m'</span><span>=></span><span>'monkey'</span><span>,</span><span>'foo'</span><span>=></span><span>'bar'</span><span>,</span><span>'x'</span><span>=></span><span> array </span><span>(</span><span>'x'</span><span>,</span><span>'y'</span><span>,</span><span>'z'</span><span>));</span><span>

$results </span><span>=</span><span> print_r</span><span>(</span><span>$b</span><span>,</span><span>true</span><span>);</span><span>// $results now contains output from print_r</span></code>
Copy after login

然后你可以使用<code>file_put_contents把$results直接写入文件:

<code><span>file_put_contents</span><span>(</span><span>'filename.txt'</span><span>,</span><span> print_r</span><span>(</span><span>$b</span><span>,</span><span>true</span><span>));</span></code>
Copy after login


参考链接:

Example from PHP manual


by iefreer


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template