-1

I'm attempting to create several arrays from a text document.

The text document is formatted as

"1","July 1999"," 2,782,546 "," $17.38 "," $338,545.98 "," 3,004 ",""

"2","August 1999"," 2,739,441 "," $18.68 "," $153,343.98 "," 3,023 ",""

"3","September 1999"," 2,650,431 "," $20.86 "," $308,929.17 "," 3,042 ",""

I need to create several arrays, that combine the date with another field such as:

$Array1 = array("July 1999"=> " 2,782,546 ", "August 1999"=> " 2,739,441 ", "September 1999"=> "2,650,431 ");

$Array2 = array("July 1999"=> " $17.38 ", "August 1999"=> " $18.68 ", "September 1999"=> "$20.86 ");

I can't figure out how to correctly parse the strings to accurately create the arrays.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 3
    Hint: It looks like a CSV file – ADyson Oct 18 '22 at 15:35
  • It does, but it is actually just a doc – Nicole Evans Oct 18 '22 at 15:43
  • What do you mean? CSV is a text format. (The file extension is irrelevant, if that's what you're thinking, it's the _format_ which is important. PHP has ready-made functions for parsing CSV-formatted data, and you can find a ton of examples, documentation and tutorials about it online already. But if by "doc" you mean "Word document" or something like that, then clearly that's not a CSV, but it's also not a "text" document either. Please clarify.) – ADyson Oct 18 '22 at 15:43
  • I was thinking it needed to be a CSV extension, I didn't realize it was the format. I will look into some CSV documentation Thanks – Nicole Evans Oct 18 '22 at 15:53
  • see https://stackoverflow.com/questions/2805427/how-to-extract-data-from-csv-file-in-php – Paul Crozer Oct 18 '22 at 15:53
  • File extensions are just a convenience so an operating system (and a human user to some extent) can guess at the file's contents and suggest the right application to open it with. They're not required and beyond what I've just described they're meaningless. Think about it - if I rename your text file with `.pptx` does that magically make it into a PowerPoint presentation? Windows might then try to open it in PowerPoint when you click it, but it doesn't mean it'll succeed. – ADyson Oct 18 '22 at 16:01
  • This will help https://www.php.net/manual/en/function.fgetcsv.php – Grumpy Oct 18 '22 at 16:15

2 Answers2

1

I was able to parse out the data I needed with this code.

$row = 1;
if (($handle = fopen("file.txt", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c = 1; $c < 2; $c++) {
            for ($d = 2; $d<3;$d++){ 
                echo $data[$c] . " " . $data[$d] . "<br />\n" ;
            }
        }
    }
}
fclose($handle);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
1

It's not clear how much flexibility you need (whether the dates / volume of data is dynamic), but you effectively need to parse the csv file lines and transpose the data.

(I'd use fopen() and fgetcsv() as you did, but I could not in the sandbox.)

Code: (Demo)

$csv = <<<TXT
"1","July 1999"," 2,782,546 "," $17.38 "," $338,545.98 "," 3,004 ",""

"2","August 1999"," 2,739,441 "," $18.68 "," $153,343.98 "," 3,023 ",""

"3","September 1999"," 2,650,431 "," $20.86 "," $308,929.17 "," 3,042 ",""
TXT;

$data = [];
// extract the csv data
foreach (
    array_map('str_getcsv', explode("\n\n", $csv))
    as
    $row
) {
    $data[$row[1]] = array_slice($row, 2, -1);
}

$result = [];
// transpose and trim
foreach ($data as $k => $row) {
    foreach ($row as $i => $v) {
        $result[$i][$k] = trim($v);
    }
}
var_export($result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136