We will simulate your solution according to the example you passed, I believe that there is a more optimized way is only one of the possibilities of implementation.
<?php
$cadastro_forncedor = [];
$cadastro_pessoa = [];
$for_pessoa = [];
// For_Pessoa = ( ID_For, ID_Pes, STATUS, NOME, FANTASIA, CPF/CNPJ, ....)
// while($row = $res->fetch()) {
// $id_for = $row['id_for'];
// $id_pes = $row['id_pes'];
// $status = $row['status'];
// //( ID_For, ID_Pes, STATUS )
// $cadastro_forncedor[$id_for] = ["id_pes" => $id_pes, "status" => $status];
// }
$cadastro_forncedor["1"] = ["id_pes" => 10, "status" => "ativo"];
$cadastro_forncedor["2"] = ["id_pes" => 20, "status" => "ativo"];
$cadastro_forncedor["3"] = ["id_pes" => 30, "status" => "ativo"];
print_r($cadastro_forncedor);
// saida
// Array
// (
// [1] => Array
// (
// [id_pes] => 10
// [status] => ativo
// )
// [2] => Array
// (
// [id_pes] => 20
// [status] => ativo
// )
// [3] => Array
// (
// [id_pes] => 30
// [status] => ativo
// )
// )
//
// while($row = $res->fetch()) {
// $id_pes = $row['id_pes'];
// $nome = $row['nome'];
// $fantasia = $row['fantasia'];
// $cpf_cnpj = $row['cpf_cnpj'];
// // ID_Pes, NOME, FANTASIA, CPF/CNPJ
// $cadastro_pessoa[$id_pes] = ["nome" => $nome,
// "fantasia" => $fantasia,
// "cpf_cnpj" => $cpf_cnpj];
// }
$cadastro_pessoa[10] = ["nome" => "Leo Caracciolo",
"fantasia" => "Caracciolo",
"cpf_cnpj" => "039.398.938-23"
];
$cadastro_pessoa[20] = ["nome" => "Vanessa",
"fantasia" => "Silva",
"cpf_cnpj" => "039.398.383-23"
];
$cadastro_pessoa[30] = ["nome" => "Maria",
"fantasia" => "Madalena",
"cpf_cnpj" => "039.476.383-23"
];
print_r($cadastro_pessoa);
//saida
// Array
// (
// [10] => Array
// (
// [nome] => Leo Caracciolo
// [fantasia] => Caracciolo
// [cpf_cnpj] => 039.398.938-23
// )
// [20] => Array
// (
// [nome] => Vanessa
// [fantasia] => Silva
// [cpf_cnpj] => 039.398.383-23
// )
// [30] => Array
// (
// [nome] => Maria
// [fantasia] => Madalena
// [cpf_cnpj] => 039.476.383-23
// )
// )
//
// vamos agora preencher nosso vetor
// com as informacoes que deseja
//
if(is_array($cadastro_forncedor)) {
foreach ($cadastro_forncedor as $id_for => $arrayForn) {
// print_r($arrayForn);
$nome = isset($cadastro_pessoa[$arrayForn["id_pes"]]["nome"]) ? $cadastro_pessoa[$arrayForn["id_pes"]]["nome"] : "";
$fantasia = isset($cadastro_pessoa[$arrayForn["id_pes"]]["fantasia"]) ? $cadastro_pessoa[$arrayForn["id_pes"]]["fantasia"] : "";
$cpf_cnpj = isset($cadastro_pessoa[$arrayForn["id_pes"]]["cpf_cnpj"]) ? $cadastro_pessoa[$arrayForn["id_pes"]]["cpf_cnpj"] : "";
// // ( ID_For, ID_Pes, STATUS, NOME, FANTASIA, CPF/CNPJ, ....)
$for_pessoa[] = [
"ID_For" => $id_for,
"ID_Pes" => $arrayForn["id_pes"],
"STATUS" => $arrayForn["status"],
"NOME" => $nome,
"FANTASIA" => $fantasia,
"CPF_CNPJ" => $cpf_cnpj
];
}
print_r($for_pessoa);
}
// saida
// Array
// (
// [0] => Array
// (
// [ID_For] => 1
// [ID_Pes] => 10
// [STATUS] => ativo
// [NOME] => Leo Caracciolo
// [FANTASIA] => Caracciolo
// [CPF_CNPJ] => 039.398.938-23
// )
// [1] => Array
// (
// [ID_For] => 2
// [ID_Pes] => 20
// [STATUS] => ativo
// [NOME] => Vanessa
// [FANTASIA] => Silva
// [CPF_CNPJ] => 039.398.383-23
// )
// [2] => Array
// (
// [ID_For] => 3
// [ID_Pes] => 30
// [STATUS] => ativo
// [NOME] => Maria
// [FANTASIA] => Madalena
// [CPF_CNPJ] => 039.476.383-23
// )
// )
By copying and pasting you will be able to run the code above and see if I’ve come close to your question.
you can run here
On the return of this query you no longer mount a Join to get the direct return you seek ?
– Vinicius Shiguemori
Good Vinicius, I use a Framework, and within this use object classes to access the tables, and in my case in particular I created a specific class to access the tables on separate basis. This Class returns me an array of the selected objects, so I have 2 arrays with the said objects. and I want to unify them.
– Marco AR Campos
Detail - Tables in different databases and different servers
– Marco AR Campos
You can already get the arrays, left just join them? Already tried the function
array_merge
?– Woss
I get it, you can use the
array_push
php within a haulforeach
and with aif
specify to relate the fields, I could even elaborate a better answer but at the moment I am busy , if they still n have sent the reply I send later– Vinicius Shiguemori
I already have the two arrays, I just need to join them, but respecting the Id_pes of the two arrays, which is the link link. I know that via programming I would have several ways to do it, as an example, in a loop of the people array, access the vendor table and increase the missing information, but this would be expensive. Since I already have the two information, I want to unite it without creating a funnel. Anderson good, the merge array_it adds at the end to another array, which is not my case.
– Marco AR Campos
Vinicius bom, the push array_works like the merge array_but your idea of if and valid. I’m here thinking with my buttons..:)
– Marco AR Campos