0

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
asked Nov 20, 2019 at 0:22

1 Answer 1

2

With this unset:

unset($fieldsToLookFor[$matchingField]);

you're trying to unset a key in the $fieldsToLookFor array like 'model' or 'url', which are its values, not its keys. (The keys are 0, 1, etc.)

In fact, it doesn't look like you would be deleting items from $matchingFields either for the same reason. It seems more likely that those were the only values added to $matchingFields in the earlier foreach ($fieldNames as $fieldName) loop.


An easy way to make this work without making many changes would be to use the fields as keys as well as values. Like this before the outer loop:

$fieldsToLookFor = array_combine($fieldsToLookFor, $fieldsToLookFor);
foreach ($files as $file){ ...

Then also later when you set $matchingFields:

if (in_array($fieldName, $fieldsToLookFor)) {
 $matchingFields[$fieldName] = $fieldName;
}
answered Nov 20, 2019 at 1:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.