Retrieve values from an array of the next line in the previous row

0

I have an array in the format below:

Array (

[0] => Array
    (
        [A] => 000001001000003001
        [B] => VLR REF COMPENSACAO DE VENDA CONFORME CO1
        [C] => 1.1.2.01.020001
        [D] => Z4
        [E] => 
        [F] => 
        [G] => 
        [H] =>           100.000,00
        [I] => 
        [J] =>       4.703.615,60 C
    )

[1] => Array
    (
        [A] => 
        [B] => MPRA DE EQUIPAMENTO DO FORNECEDOR PARANA 1
        [C] => 
        [D] => 
        [E] => 
        [F] => 
        [G] => 
        [H] => 
        [I] => 
        [J] => 
    )

[2] => Array
    (
        [A] => 
        [B] => EQUIPAMENTOS S.A. - NF VENDA 000000261 1
        [C] => 
        [D] => 
        [E] => 
        [F] => 
        [G] => 
        [H] => 
        [I] => 
        [J] => 
    )
[3] => Array
    (
        [A] => 008850001000002098
        [B] => PG. DOC. 3 000113169  TP:NF FORNEC.:0000
        [C] => 
        [D] => Y1
        [E] => 
        [F] => 
        [G] => 
        [H] =>             2.188,35
        [I] => 
        [J] =>           8.901,01 C
    )

[4] => Array
    (
        [A] => 
        [B] => 38-PARANA EQUIPAMENTOS S.A.
        [C] => 
        [D] => 
        [E] => 
        [F] => 
        [G] => 
        [H] => 
        [I] => 
        [J] => 
    )       

)

I need the array to be in another format, as shown below:

Array (

[0] => Array
    (
        [A] => 000001001000003001
        [B] => VLR REF COMPENSACAO DE VENDA CONFORME CO1 MPRA DE EQUIPAMENTO DO FORNECEDOR PARANA 1 EQUIPAMENTOS S.A. - NF VENDA 000000261 1
        [C] => 1.1.2.01.020001
        [D] => Z4
        [E] => 
        [F] => 
        [G] => 
        [H] =>           100.000,00
        [I] => 
        [J] =>       4.703.615,60 C
    )

[1] => Array
    (
        [A] => 
        [B] => 
        [C] => 
        [D] => 
        [E] => 
        [F] => 
        [G] => 
        [H] => 
        [I] => 
        [J] => 
    )

[2] => Array
    (
        [A] => 
        [B] => 
        [C] => 
        [D] => 
        [E] => 
        [F] => 
        [G] => 
        [H] => 
        [I] => 
        [J] => 
    )
[3] => Array
    (
        [A] => 008850001000002098
        [B] => PG. DOC. 3 000113169  TP:NF FORNEC.:0000 38-PARANA EQUIPAMENTOS S.A.
        [C] => 
        [D] => Y1
        [E] => 
        [F] => 
        [G] => 
        [H] =>             2.188,35
        [I] => 
        [J] =>           8.901,01 C
    )

[4] => Array
    (
        [A] => 
        [B] => 
        [C] => 
        [D] => 
        [E] => 
        [F] => 
        [G] => 
        [H] => 
        [I] => 
        [J] => 
    )       

)

Any idea how I could do this?

If I asked the question wrongly, this is the first time I ask.

    
asked by anonymous 26.10.2018 / 18:40

1 answer

0

Is not a loop of repetition enough?

Considering that your array is declared this way:

$arr = [
    [
        'A' => '000001001000003001',
        'B' => 'VLR REF COMPENSACAO DE VENDA CONFORME CO1',
        'C' => '1.1.2.01.020001',
        'D' => 'Z4',
        'E' => '',
        'F' => '',
        'G' => '',
        'H' => '',
        'I' => '100.000,00',
        'J' => '4.703.615,60 C'
    ],[
        'A' => '',
        'B' => 'MPRA DE EQUIPAMENTO DO FORNECEDOR PARANA 1',
        'C' => '',
        'D' => '',
        'E' => '',
        'F' => '',
        'G' => '',
        'H' => '',
        'I' => '',
        'J' => ''
    ],[
        'A' => '',
        'B' => 'EQUIPAMENTOS S.A. - NF VENDA 000000261 1',
        'C' => '',
        'D' => '',
        'E' => '',
        'F' => '',
        'G' => '',
        'H' => '',
        'I' => '',
        'J' => ''
    ]
];

It would be enough to go through the arrays, concatenating the values with the first item of the array, and then passing an empty string to the current item.

for ($i = 1; $i < count($arr); ++$i) {
    foreach ($arr[$i] as $chave => $valor) {
        if ($valor !== '') {
            $arr[0][$chave] .= " $valor";
            $arr[$i][$chave] = '';      
        }
    }
}
    
26.10.2018 / 19:31