0

The first array has the same keys as those in the sheet key content in the second array.

I would need to edit the key element of the first array, with the contents that I have in the key table in the second array, according to the keysheet

I tried in countless ways but without success, here is an illustrative image

the first:

    Array
    (
        [0] => Array
            (
                [CIDADES] => PONTA GROSSA
                [PERIODO] => 12:00 - 18:00
                [ID] => Z8932
                [END] => AV VSC DE MAUA,, ALT 
                [CONTRATO] => 7947488
                [AREA] => AR01
                [ASSINANTE] => DAVI
                [BAIRRO] => OFICINAS
                [OBS] => [Agendamento par]  
                [TEL RES] => 42410435
                [TEL COM] => 
            )

        [1] => Array
            (
                [CIDADES] => PONTA GROSSA
                [PERIODO] => 08:00 - 12:00
                [ID] => Z7526
                [END] => R P1000, FD 
                [CONTRATO] => 799644
                [AREA] => AR01
                [ASSINANTE] => BEATRIZ
                [BAIRRO] => NEVES
                [OBS] => [Agendamento]  
                [TEL RES] => 42988674761
                [TEL COM] => 
            )

        [2] => Array
            (
                [CIDADES] => PONTA GROSSA
                [PERIODO] => 08:00 - 12:00
                [ID] => Z0979
                [END] => R J93, FD 
                [CONTRATO] => 79490
                [AREA] => AR01
                [ASSINANTE] =>FERNANDES  REIS
                [BAIRRO] => UVARANAS
                [OBS] => [Agendamentoente] 
                [TEL RES] => 4289986
                [TEL COM] => 
            )
    )

And the second

    Array
    (
        [0] => Array
            (
                [id] => 1
                [table] => id_operacao
                [sheet] => CIDADES
            )

        [1] => Array
            (
                [id] => 2
                [table] => horario
                [sheet] => PERIODO
            )

        [2] => Array
            (
                [id] => 3
                [table] => tipo_wo
                [sheet] => TIPO DE SERVICO
            )

        [3] => Array
            (
                [id] => 4
                [table] => tecnico_id
                [sheet] => ID
            )

        [4] => Array
            (
                [id] => 5
                [table] => contrato
                [sheet] => CONTRATO
            )

        [5] => Array
            (
                [id] => 6
                [table] => roteamento
                [sheet] => AREA
            )

        [6] => Array
            (
                [id] => 7
                [table] => endereco
                [sheet] => END
            )

        [7] => Array
            (
                [id] => 8
                [table] => nome_cliente
                [sheet] => ASSINANTE
            )

        [8] => Array
            (
                [id] => 9
                [table] => bairro
                [sheet] => BAIRRO
            )

        [9] => Array
            (
                [id] => 10
                [table] => obs1
                [sheet] => OBS
            )

        [10] => Array
            (
                [id] => 11
                [table] => telefone
                [sheet] => TEL RES
            )

        [11] => Array
            (
                [id] => 12
                [table] => celular
                [sheet] => TEL COM
            )
    )

enter image description here

1 Answer 1

1

Hey man I skipped a few items in the array but hopefully I got the structure right ;)

$firstArray = [
    [
        'CIDADES' => 'PONTA GROSSA',
        'PERIODO' => '12:00 - 18:00',
        'ID' => 'Z8932',
        'END' => 'AV VSC DE MAUA,, ALT',
        'CONTRATO' => '7947488',
        'AREA' => 'AR01',
        'ASSINANTE' => 'DAVI',
        'BAIRRO' => 'OFICINAS',
        'OBS' => ['Agendamento par'],
        'TEL RES' => '42410435',
    ],
    [
        'CIDADES' => 'PONTA GROSSA',
        'PERIODO' => '08:00 - 12:00',
        'ID' => 'Z7526',
        'END' => 'R P1000, FD',
        'CONTRATO' => '799644',
        'AREA' => 'AR01',
        'ASSINANTE' => 'DAVI',
        'BAIRRO' => 'BEATRIZ',
        'OBS' => ['Agendamento'],
        'TEL RES' => '42988674761',
    ],
    [
        'CIDADES' => 'PONTA GROSSA',
        'PERIODO' => '08:00 - 12:00',
        'ID' => 'Z0979',
        'END' => 'R J93, FD',
        'CONTRATO' => '79490',
        'AREA' => 'AR01',
        'ASSINANTE' => 'FERNANDES  REIS',
        'BAIRRO' => 'UVARANAS',
        'OBS' => ['Agendamentoente'],
        'TEL RES' => '4289986',
    ],
];

$secondArray = [
    [
        'id' => 1,
        'table' => 'id_operacao',
        'sheet' => 'CIDADES',
    ],
    [
        'id' => 2,
        'table' => 'horario',
        'sheet' => 'PERIODO',
    ],
    [
        'id' => 3,
        'table' => 'tipo_wo',
        'sheet' => 'TIPO DE SERVICO',
    ],
    [
        'id' => 4,
        'table' => 'tecnico_id',
        'sheet' => 'ID',
    ],
    [
        'id' => 5,
        'table' => 'contrato',
        'sheet' => 'CONTRATO',
    ],
    [
        'id' => 6,
        'table' => 'roteamento',
        'sheet' => 'AREA',
    ],
    [
        'id' => 7,
        'table' => 'endereco',
        'sheet' => 'END',
    ],
    [
        'id' => 8,
        'table' => 'nome_cliente',
        'sheet' => 'ASSINANTE',
    ],
];

//I guess this is what you were missing most
$oldKeysToNewKeys = array_combine(
    array_column($secondArray, 'sheet'),
    array_column($secondArray, 'table')
);

foreach ($firstArray as $key => $firstArrayElements) {
    $newFirstArrayElement = [];
    foreach ($firstArrayElements as $oldKey => $value) {
        //You could add checks to avoid errors when keys are not set etc..
        $newKey = $oldKeysToNewKeys[$oldKey];
        $newFirstArrayElement[$newKey] = $value;
    }
    $firstArray[$key] = $newFirstArrayElement;
}

//There you go!
var_dump($firstArray);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.