Create an array from 2 other arrays

1

I have two distinct tables, which are in distinct database, and I need an array with unified information.

Example:

Cadastro de Fornecedores ( ID_For, ID_Pes, STATUS )

Cadastro de Pessoas ( ID_Pes, NOME, FANTASIA, CPF/CNPJ, ... )

I need an array as below:

For_Pessoa = ( ID_For, ID_Pes, STATUS, NOME, FANTASIA, CPF/CNPJ, ....)

Note: Remembering that in the join ID_Pes has to match, the People array only contains the data of the suppliers. (used "IN" of Fornecedores->ID_Pes )

    
asked by anonymous 20.10.2017 / 22:54

3 answers

0

As quoted in the comments, you can make one condition, see this is an example, you can have others.

$Fornecedores = [
  [ 'ID_For' => 1, 'ID_Pes' => 1, 'STATUS' => 1 ],
  [ 'ID_For' => 2, 'ID_Pes' => 2, 'STATUS' => 2 ],
  [ 'ID_For' => 3, 'ID_Pes' => 3, 'STATUS' => 3 ],
];
$Pessoas = [
  [
    'ID_Pes' => 1,
    'NOME' => 'Pessoa 1',
    'FANTASIA' => 'Fantasia 1',
    'CPF/CNPJ' => '111.111.111-11'
  ],
  [
    'ID_Pes' => 2,
    'NOME' => 'Pessoa 2',
    'FANTASIA' => 'Fantasia 2',
    'CPF/CNPJ' => '222.222.222-22'
  ],
  [
    'ID_Pes' => 3,
    'NOME' => 'Pessoa 3',
    'FANTASIA' => 'Fantasia 3',
    'CPF/CNPJ' => '333.333.333-33'
  ],
];

$For_Pessoa = [];

for ($i = 0; $i < count($Fornecedores); $i++) {
  // Verifica se ID_Pes de ambos são iguais
  if ($Fornecedores[$i]['ID_Pes'] == $Pessoas[$i]['ID_Pes']) {
    // Combina os dois resultados e gurda na variável.
    $ForPessoa = array_merge($Fornecedores[$i], $Pessoas[$i]);
    // Adiciona na nova Array
    array_push($For_Pessoa, $ForPessoa);
  }
}

print_r($For_Pessoa);

The output will be:

Array
(
    [0] => Array
        (
            [ID_For] => 1
            [ID_Pes] => 1
            [STATUS] => 1
            [NOME] => Pessoa 1
            [FANTASIA] => Fantasia 1
            [CPF/CNPJ] => 111.111.111-11
        )

    [1] => Array
        (
            [ID_For] => 2
            [ID_Pes] => 2
            [STATUS] => 2
            [NOME] => Pessoa 2
            [FANTASIA] => Fantasia 2
            [CPF/CNPJ] => 222.222.222-22
        )

    [2] => Array
        (
            [ID_For] => 3
            [ID_Pes] => 3
            [STATUS] => 3
            [NOME] => Pessoa 3
            [FANTASIA] => Fantasia 3
            [CPF/CNPJ] => 333.333.333-33
        )

)
  

You can see it working here at repl.it

    
21.10.2017 / 00:20
0

Let's simulate your solution as the example you passed, I believe that there is a more optimized way is just one of the implementation possibilities.

<?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
//         )
// )

Copying and pasting you will be able to run the above code and see if I came close to your question.

  

you can run here

    
21.10.2017 / 00:44
0

First I want to apologize for the delayed return, short weeks and long holidays:)).

Second to thank everyone for the help they provided, they were of great value to my conclusion and solution. So I did not choose one, but I scored all of them. (I do not know if I did it right.)

Third Solution:

1 - I have loaded all the active providers (STATUS) into an array, since this is technically smaller in terms of registers (Base).

$Fors= array();
$Foreg = array();

if ( $Fors_db = "Usei Classe para fazer o Select e retornar a informação" )
{
   foreach( $Fors_db as $For )
   {
       $Fors[] = $For->FOR_PESSOAID;

       $Foreg[ $For->FOR_PESSOAID ] = $For;
   } 
} 
else $Fors[] = '0';

2 - I loaded all the People, which has a link with the Suppliers $ Fors [], that is, I used an (IN) in the select. In a foreach I loaded a datagrid by adding the Vendor information.

if ( $objects = "Usei Classe para fazer o Select e retornar a informação" )
{
    foreach ( $objects as $object )
    {
        $object->FOR_ID      = $Foreg[ $object->PES_ID ]->FOR_ID;
        $object->PES_TIPO    = $TpPess[ $object->PES_TIPO ];
        $object->PES_CPFCNPJ = TGeral::CpfCnpj( $object->PES_CPFCNPJ );
        $object->PES_CEP     = TGeral::Cep( $object->PES_CEP );
        $object->PES_STATUS  = $Foreg[ $object->PES_ID ]->FOR_STATUS;
        $object->PES_ULTIMA  = TDate::date2br( $object->PES_ULTIMA );

        datagrid->addItem( $object );
    }
}

Att.

MarcoARCampos.

    
14.11.2017 / 00:35