我想从目录中的文本文件中提取值,并将这些值列表放入数组中。
我不确定如何将这些数组安排到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 :(得分: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;
}
(对象)强制转换将帮助您将数组转换为对象。