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.
2
Answers
I was able to parse out the data I needed with this code.
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()
andfgetcsv()
as you did, but I could not in the sandbox.)Code: (Demo)