我有一个txt文件,其中包含如下数组:
array (
0 => 'jack',
1 => 'alex'
)
我想在我的PHP代码中使用这个数组,但它不像这个返回a
的例子那样工作
$list= file_get_contents(myfile.txt);
echo $list[0];
如何转换?
在该文件中使用数组serialize
d或json_encode
d。然后,在读取时,只需$list
中的unserialize
或json_decode
示例:
$list = unserialize(file_get_contents(myfile.txt));
要回答问题,eval
看起来会处理数组(数组已使用var_export输出?)
但是执行未知代码是个坏主意,W0rlart有更好的方法(json是我的首选)