2009-11-02 77 views
0

我想从目录中的文本文件中提取值,并将这些值列表放入数组中。php:模拟mysql结果集

但我不知道如何将这些数组安排到mysql结果集“like”对象中。

这是我到目前为止有:

// Retrieve all files names, create an array 
$dir = './textfiles/'; 
$files1 = glob($dir . '*' . '.txt'); 

foreach ($files1 as $filename) 
{ 
    $file_array[] = basename($filename); 
} 

//use the file function to create arrays from the text files 
$filepath = $dir . $file_array[0]; 
$text_array = file($filepath); 

上面的代码只检索从1个文本文件中的信息。我怎样才能将其他值检索到其他数组?
其次一旦所有的数组被检索到,一个对象怎么做?
第三,我怎样才能使这个函数适应新创建的文本文件?

+0

你还没有提及什么文件格式。根据你想要的功能,处理这个问题最简单的方法是编写你的INSERT脚本并在数据库上运行它们以填充表格。 – 2009-11-02 01:06:08

回答

0

首先你写

$filepath = $dir.$file_array[0]; 
$text_array = file($filepath); 

这段代码只能从第一个文件

您将通过阵列需要循环创建数组。简单地说:

// Retrieve all files names, create an array 
$dir = './textfiles/'; 
$files1 = glob($dir . '*' . '.txt'); 

$arr_of_objects = array(); 

foreach ($files1 as $filename) 
{ 
    $arr = file($filename); 
    $arr_of_objects[] = (object)$arr; 
} 

(对象)铸造将帮助您将数组转换为对象。