php实例之将文件读入到php数组中

时间:2016-08-15
本文章向大家介绍php如何以文件行为单元将文件读入到php数组中,需要的朋友可以参考一下本文章的源代码。

php实例之将文件读入到php数组中,源代码如下:

saveme.txt文件内容如下:

a@example.com|Alice
b@example.org|Bill
c@example.com|Charlie
d@example.com|Lewis

php读取文件到数组:

<?php
$file= file( 'saveme.txt' );
$file_array=array();
/* http://www.manongjc.com/article/1355.html */
while ( list( $line_num, $line ) = each( $file ) ) :
     $file_array[]=htmlspecialchars( $line );
endwhile;
print_r($file_array);
?>

运行结果:

Array
(
    [0] => a@example.com|Alice

    [1] => b@example.org|Bill

    [2] => c@example.com|Charlie

    [3] => d@example.com|Lewis
)