<?php
$csv = <<<CSV
"Price","Bogus","Symbol","Description","Qty (Quantity)","Price Chng % (Price Change %)","Price Chng $ (Price Change $)","Day Chng % (Day Change %)","Day Chng $ (Day Change $)","Cost Basis","Gain % (Gain/Loss %)","Gain $ (Gain/Loss $)","Reinvest?","Reinvest Capital Gains?","Last Div (Last Dividend)","Volume","Security Type"
"$19.39","foo1","Test1","ETF","820","-2.42%","-$0.48","-2.42%","-$393.6","$601.73","-18.89%","-$3701.93","No","--","$0.72025","14,626,464","ETFs & Closed End Funds"
"$49.43","foo2","Test2"," ETF","110","-2.39%","-$1.21","-2.39%","-$133.1","$995.2","-9.31%","-$557.9","Yes","--","N/A","59,351,095","ETFs & Closed End Funds"
"$21.77","foo3","Test3"," ETF","760","-3.72%","-$0.84","-3.72%","-$638.4","$687.73","-20.02%","-$142.53","No","--","$2.0216","6,402,084","ETFs & Closed End Funds"
CSV;
$handle = tmpfile();
fwrite($handle, $csv);
rewind($handle);
$whitelist = array(
"Symbol",
"Description",
"Qty (Quantity)",
"Price",
"Price Chng % (Price Change %)",
"Price Chng $ (Price Change $)",
"Day Chng % (Day Change %)",
"Day Chng $ (Day Change $)",
"Cost Basis",
"Gain % (Gain/Loss %)",
"Gain $ (Gain/Loss $)",
"Reinvest?",
"Reinvest Capital Gains?",
"Last Div (Last Dividend)",
"Volume",
"Security Type"
);
$headers = fgetcsv($handle);
if ($headers) {
$map = array_flip($headers);
}
$result = array();
while (($values = fgetcsv($handle)) !== false) {
$row = array();
foreach ($whitelist as $col) {
$row[$col] = isset($map[$col],$values[$map[$col]]) ? $values[$map[$col]] : null;
}
$result[] = $row;
}
fclose($handle);
var_export($result);
preferences:
25.82 ms | 410 KiB | 5 Q