php: simulate mysql result set
I'd like to pull values from textfiles in a directory and put these lists of values into arrays.
I am not sure however how to arrange these arrays into a mysql result set "like" object.
Here is what I have so far:
// 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);
The code above only retrieves the information from 1 text file. How can I retrieve the other values into additional arrays?
Secondly once all the arrays are retrieved, how can a make开发者_Python百科 the object? Thirdly, how can I make this a function to accomodate newly created textfiles?Firstly you wrote
$filepath = $dir.$file_array[0];
$text_array = file($filepath);
this piece of code only create array from the first file
You will need to loop through the array. Simply:
// 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;
}
The (object) casting will help you to convert array to object.
精彩评论