Recover values from an array of the next line in the previous line

Asked

Viewed 36 times

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] => 
    )       

)

And I need the array to be in another format, as an example 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?

In case I asked the question wrong, this is the first time I ask.

  • I don’t understand what you want to do. You want to Pear only one of the array elements?

  • This array is mounted by Phpexcel. The spreadsheet has more records, but I only put these as examples. I will edit the question with more record to try to make more clear what I am trying to do.

  • I don’t understand what you’re trying to do either

  • What we didn’t understand was your question, "take a line value in the previous line". Try to make your question clearer so we can understand.

1 answer

0

A loop of repetition is not enough?

Whereas 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' => ''
    ]
];

Just scroll through the arrays, concatenating the values with the first array item, 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] = '';      
        }
    }
}

Browser other questions tagged

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