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!!
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
Could you help me create this array correctly or give me some other idea to make this validation ?
Thanks for the reply, but is there any way to know(identify) who is duplicated?
– Wagner Fillio
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
– Wagner Fillio
With what values you tested that doubled, even without existing?
– Lucius
I tested the two values that are in the image, in the first image that contains Excel
– Wagner Fillio
How strange, can send the result of
echo json_encode($cpfs);
?– Lucius
https://imgur.com/MJ95FuE
– Wagner Fillio
Ahh yes, indeed, I had misunderstood. I edited the answer, now it must be what you expect.
– Lucius
My return now is: array_key_exists(): The first argument should be either a string or an integer ...
– Wagner Fillio
Let’s go continue this discussion in chat.
– Lucius
I changed the data type of the variable
cpf_cnpj
and it all worked out, thanks for your help!!– Wagner Fillio