fgetcsv vs fget??
Posted: Tue Jan 15, 2013 10:30 pm
Hi
I had a piece of code to put a csv file into a multidimensional array. Then I found out that the files I will process are txt file with records in the file separated by broken bars " | ". (e.g. 1000007414¦Abela¦John¦Andrew¦MR¦01-JUL-1983¦1¦1¦4¦2079¦2¦19¦¦1101¦42849231751¦ U)
Can someone show me how to 'fget' the txt files?
This is the code I had for csv files -
Now the file is txt file, I should change "fgetcsv($handle,,)" to something else?
I had a piece of code to put a csv file into a multidimensional array. Then I found out that the files I will process are txt file with records in the file separated by broken bars " | ". (e.g. 1000007414¦Abela¦John¦Andrew¦MR¦01-JUL-1983¦1¦1¦4¦2079¦2¦19¦¦1101¦42849231751¦ U)
Can someone show me how to 'fget' the txt files?
This is the code I had for csv files -
Code: Select all
if (($handle = fopen("file1.csv", "r")) !== FALSE) {
# Set the parent multidimensional array key to 0.
$nn1 = 0;
while (($data = fgetcsv($handle, 0, "|")) !== FALSE) {
# Count the total keys in the row.
$c1 = count($data);
# Populate the multidimensional array.
for ($x=0;$x<$c1;$x++)
{
$csvarray1[$nn1][$x] = $data[$x];
echo "$csvarray1($nn1)($x) = ". $csvarray1[$nn1][$x] . "<br />\n";
echo "<br />\n";
}
$nn1++;
echo "<br />\n";
}
# Close the File.
fclose($handle);
}Code: Select all
if (($handle = fopen("personQh0001.txt", "r")) !== FALSE) {
# Set the parent multidimensional array key to 0.
$nn1 = 0;
while (($data = fgetcsv($handle, 0)) !== FALSE) {
# Count the total keys in the row.
$c1 = count($data);
# Populate the multidimensional array.
for ($x=0;$x<$c1;$x++)
{
$csvarray1[$nn1][$x] = $data[$x];
//echo "$csvarray1($nn1)($x) = ". $csvarray1[$nn1][$x] . "<br />\n";
//echo "<br />\n";
}
$nn1++;
//echo "<br />\n";
}
# Close the File.
fclose($handle);
}