Create Array to validate duplicate CPF in a repeat loop in PHP
Get the solution ↓↓↓My main goal is toimport
data from anExcel spreadsheet
intoMysql 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 ofvalidation
!
For example!
Validate that the sameCPF exists for two different customers
.
Below is an example of the Excel spreadsheet !!
CPF is the unique identification document of each Brazilian citizen
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'
]
UpdateI 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
Answer
Solution:
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;
}
}
Answer
Solution:
Yes, you are right. Array does not contain same key. But you can use Multi-dimension array to short this problem.
But as you said, export data in database. So you cannot use
CPF
key as a primary key or unique key.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.