Create array to validate duplicate CPF in a PHP Loop

Asked

Viewed 143 times

0

My primary goal is importar data of a planilha em Excel to the Mysql using PHP.

I completed this goal and can already import all columns from Excel to the respective tabelas of the database!

My next goal is to make a sort of validação!

For example!

Validate whether um mesmo CPF existe para dois clientes diferentes.

Below is an example of the Excel spreadsheet!! inserir a descrição da imagem aqui

Below I have a sample of the function which results the column and rows of the spreadsheet!

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 a array, using the CPF as chave and the client’s name as valor; then would validate whether the chave array repeats and, if positive, would return false and stop importing.

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

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

Follow images of how it is returning, using the print_r of PHP

print 1 and 2, according to the function code, respectively inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Could you help me create this array correctly or give me some other idea to make this validation ?

1 answer

1


The way you thought, it won’t work, because it will never be possible to put two identical keys in an array, with different values.

EDIT

Since the idea is just "validate whether the same CPF exists for two different clients" and if there are duplicates, stop the import, you can do so:

$cpfs = array();
for($row = 2; $row <= $highestRow; $row++){
  $cpf_cnpj = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
  $name     = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
  if(array_key_exists($cpf_cnpj, $cpfs) && $cpfs[$cpf_cnpj] != $name){
    echo "CPF/CNPJ duplicado: " . $cpf_cnpj;
  }else{
    $cpfs[$cpf_cnpj] = $name;
  }
}
  • Thanks for the reply, but is there any way to know(identify) who is duplicated?

  • And testing the code, I noticed that it’s returning duplicated, even when there’s no duplicity. In this case the CPF can even exist on more than one line, which cannot eh have two different names with the same CPF

  • With what values you tested that doubled, even without existing?

  • I tested the two values that are in the image, in the first image that contains Excel

  • How strange, can send the result of echo json_encode($cpfs);?

  • https://imgur.com/MJ95FuE

  • Ahh yes, indeed, I had misunderstood. I edited the answer, now it must be what you expect.

  • My return now is: array_key_exists(): The first argument should be either a string or an integer ...

  • I changed the data type of the variable cpf_cnpj and it all worked out, thanks for your help!!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.