Peter
Peter

Reputation: 173

Php Unset not deleting array elements

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

Upvotes: 0

Views: 867

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

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;
}

Upvotes: 2

Related Questions