Skip to content
Advertisement

Create Array to validate duplicate CPF in a repeat loop in PHP

My main goal is to import data from an Excel spreadsheet into Mysql using PHP.

I completed this goal and now I can import all the columns from Excel to the respective database tables!

My next goal is to do some kind of validation!

For example!

Validate that the same CPF exists for two different customers.

Below is an example of the Excel spreadsheet !!

CPF is the unique identification document of each Brazilian citizen

enter image description here

Below is an example of the function that results in the plan excel column and row data

private function import_file($file)
    {
        $path   = $file;
        $object = PHPExcel_IOFactory::load($path);

        foreach($object->getWorksheetIterator() as $worksheet)
        {
            $highestRow    = $worksheet->getHighestRow();
            $highestColumn = $worksheet->getHighestColumn();

            $person_array_testing = [];
            for($row = 2; $row <= $highestRow; $row++)
            {
                # array testing
                $cpf_cnpj = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
                $name     = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
                $contract = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
                $invoice  = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
                $document = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
                $value    = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
                $expiry   = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
                $address  = $worksheet->getCellByColumnAndRow(7, $row)->getValue();
                $phone    = $worksheet->getCellByColumnAndRow(8, $row)->getValue();
                $email    = $worksheet->getCellByColumnAndRow(9, $row)->getValue();

                // print 1
                // pr($cpf_cnpj);
                // pr($name);

                if( ! empty($cpf_cnpj))
                {
                    $keys = [$cpf_cnpj];
                    $new_array = array_fill_keys($keys, $name);
                    // print 2
                    pr($new_array); 
                }                
                # array testing
            }
        }
    }

My idea to validate duplicate CPF was to create an array, using the CPF as the key and the customer’s name as the value; then it would validate if the array key is repeated and, if so, it would return false and interrupt the import.

I believe that, in the case of a duplicate CPF, the expected array was this;

[
    '11111111177' => 'MARIA DE LOURDES CAETANO',
    '11111111177' => 'ADRIENE FARIA MARTINS CONRADO DOS SANTOS'
]

Update I don't think it would work because an array cannot have repeated keys

Here are images of how to return, using or print_r from PHP print 1 and 2, as it is in the function code, respectively
enter image description here enter image description here

Advertisement

Answer

Your idea for the validation is correct, but you are failing to implement it.

You can try something like this:

$validationArray = [];
if (!empty($cpf_cnpj)) {
    if (array_key_exists($cpf_cnpj, $validationArray) {
        return false;
    }

    $validationArray[$cpf_cnpj] = $name;
}

Or, if you don’t want to interrupt the processing at each duplication, you can store the duplications in a different array and deal with them after importing:

$validationArray = [];
$duplicationsArray = [];
if (!empty($cpf_cnpj)) {
    if (array_key_exists($cpf_cnpj, $validationArray) {
        $duplicationsArray[$cpf_cnpj] = $name;
    } else {
        $validationArray[$cpf_cnpj] = $name;
    }
}

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement