More or less a Select in array

1

Good evening, I have two Arrays:

$array1 = ( 

[0] => Array ("Nome":"Rodrigo","CPF":"123456789-00","Cargo":"Abestado")
[1] => Array ("Nome":"Maria","CPF":"987654321-00","Cargo":"Abestada")

$array2 = ( 

[0] => Array ("CPF":"123456789-00","Titulo de Eleitor":"456789123")
[1] => Array ("CPF":"987654321-00","Titulo de Eleitor":"987564123")

In these arrays I have many records. What I want to know is if you can make an appointment for the CPF between the arrays. So I look for $ array $ CPF1 = CPF from $ array2. When the result is TRUE, I want the data that differs between arrays to be imbued in $ array3:

$array3 = ( 

[0] => Array ("Nome":"Rodrigo","CPF":"123456789-00","Cargo":"Abestado","Titulo de Eleitor":"987564123")
[1] => Array ("Nome":"Maria","CPF":"987654321-00","Cargo":"Abestada","Titulo de Eleitor":"987564123")

Is this possible?

Thanks in advance for your help

    
asked by anonymous 02.08.2018 / 04:48

3 answers

2

Another form that values performance more is:

  • Create an auxiliary array based on array2 that uses cpf as the key for the search to be done in time, O (1).
  • Go through each element of array1 and find cpf in array2 add the join of the two arrays to the final array through the function array_merge

Implementation:

$arrayChaves = Array(); //array com cpfs como chaves
foreach($array2 as $valor){
    $chave = $valor["CPF"];
    $arrayChaves[$chave] = $valor;
    unset($arrayChaves[$chave]["CPF"]); //retirar cpf dos valores parar ficar só como chave
}

$arrayFinal = Array();
foreach($array1 as $pessoa){
    $cpfPessoa = $pessoa["CPF"];
    if (isset($arrayChaves[$cpfPessoa])){ //se o cpf existe no arrayChaves
        //adiciona a junção dos dois arrays
        $arrayFinal[] = array_merge($pessoa, $arrayChaves[$cpfPessoa]);
    }
}

See this example working on Ideone

In this example the result was built into another array that I called $arrayFinal . If you want you can also change the $array1 you have directly by rewriting a last for :

foreach($array1 as $pos => $pessoa){ //agora com chave e valor
    $cpfPessoa = $pessoa["CPF"];
    if (isset($arrayChaves[$cpfPessoa])){ //se o cpf existe no arrayChaves
        //a modificação é feita pelo array1 e a sua chave
        $array1[$pos] = array_merge($pessoa, $arrayChaves[$cpfPessoa]);
    }
}

See this example on Ideone

    
02.08.2018 / 12:50
1

You can use array_column to get all values of an array of a specific key along with array_search to find out if the search value exists. And then insert into the new array:

    $array1 =   array( 
        array ("Nome" => "Rodrigo","CPF" => "123456789-00","Cargo"=>"Abestado"),
        array("Nome" => "Andrei","CPF" => "12312313-00","Cargo"=>"Abestado em Programação")
    );
    $array2 =array(array ("CPF"=>"123456789-00","Titulo de Eleitor"=>"456789123")) ;

    $array3 = array();

    $colunas = array_column($array2, 'CPF');

    foreach($array1 as $pessoas){
        if(($key = array_search($pessoas['CPF'],$colunas)) !== false)
            $array3[] = array(
                "Nome" => $pessoas['Nome'], 
                "CPF" => $pessoas['CPF'], 
                "Cargo" => $pessoas['Cargo'],
                "Titulo de Eleitor" =>  $array2[$key]['Titulo de Eleitor']
            );

    }

    print_r($array3);

Output will be:

  

Array ([0] => Array ([Name] => Rodrigo [CPF] => 123456789-00 [Position]   = > Abestado [Voter Registration] = > 456789123))

    
02.08.2018 / 06:17
0

Edited:

  

No Firmware:

$array3 = array_replace_recursive($array1,$array2);
    
02.08.2018 / 05:35