Create an array from 2 other arrays

Asked

Viewed 203 times

1

I have two separate tables, which are in a separate database, and I need an array of 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 at the junction o ID_Pes has to match, the People array only contains vendor data. ( used "IN" of Fornecedores->ID_Pes )

  • On the return of this query you no longer mount a Join to get the direct return you seek ?

  • 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.

  • Detail - Tables in different databases and different servers

  • You can already get the arrays, left just join them? Already tried the function array_merge?

  • I get it, you can use the array_push php within a haul foreach and with a if 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

  • 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.

  • Vinicius bom, the push array_works like the merge array_but your idea of if and valid. I’m here thinking with my buttons..:)

Show 2 more comments

3 answers

0

As quoted in the comments, you can make a 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 exit 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 on repl it.

  • do not want to create another question, could you help me in this dúida on array ? https://answall.com/questions/248059/rettinglinkeddata

0

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

0

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

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

Third Solution:

1 - I loaded all Active Vendors (STATUS) into an array, since this is technically lower in Record Number (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 clicked on an array all People, which has a link with Suppliers $Fors[], that is to say I used a (IN) in select. In a "foreach" I loaded a datagrid 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.

Browser other questions tagged

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