I'm going through csv files and am trying to delete elements in 2 arrays (matchingFields &fieldstolookfor) as I find their elements in a csv.
public function addRecords($dealerFeedsDirectory, $vin)
{
$files = glob($dealerFeedsDirectory . "*.csv");
$fieldsToLookFor = array("dealername", "dealeraddress", "dealerstate", "year", "make", "model", "phone", "url");
$fieldsFound = array();
foreach ($files as $file) {
if(empty($fieldsToLookFor)) {
break;
}
echo "File We are Looking through : " . $file , "<br>";
echo "Fields left : ";
print_r($fieldsToLookFor);
echo " <br>";
$fp = fopen($file, 'r');
$csvReader = new CSVReader($fp, ',');
if ($fieldNames = $csvReader->readHeader()) {
//print_r( $fieldNames);
$matchingFields = array();
foreach ($fieldNames as $fieldName) {
if (in_array($fieldName, $fieldsToLookFor)) {
$matchingFields[] = $fieldName;
}
}
//print_r($matchingFields);
while (($row = $csvReader->readRow()) !== false && !empty($matchingFields)) {
if ($row[Constants::VIN] == $vin) {
foreach ($matchingFields as $matchingField) {
$fieldsFound[$matchingField] = $row[$matchingField];
print_r( $matchingFields);echo "matchingfields <br>";
print_r( $fieldsToLookFor);echo "fieldstolookfor <br>";
unset($matchingFields[$matchingField]);
unset($fieldsToLookFor[$matchingField]);
}
break;
}
}
}
}
print_r($fieldsFound);
}
Items are being deleted from the $matchingFields array but not the $fieldsToLookFor (fieldsToLookfor should also have 5 items) array after going through one csv file. Why is this?
Array ( [0] => url [1] => make [2] => model [3] => year [4] => phone ) matchingfields
Array ( [0] => dealername [1] => dealeraddress [2] => dealerstate [3] => year [4] => make [5] => model [6] => phone [7] => url ) fieldstolookfor