add to array after loop

0

I have some loops in my php and I want to get an array like this:

Array ( [nome1] => valor [nome2] => valor ) 

Within a loop I've tried: $array_dos_pagamentos[nome] = $variavelnome; and in another loop $array_dos_pagamentos[valor] = $variavelvalor;

The problem is that the output Array ( [nome] => Susana laginha, [valor] => 84 ) or just a log .. if I do:

$array_dos_pagamentos[]=$variavelnome;
$array_dos_pagamentos[]=$variavelvalor; 

sai

Array ( 
        [0] => Beatriz grade 
        [1] => 15 
        [2] => Promotores 
        [3] => 40 
        [4] => Susana laginha 
        [5] => 84 
) 
    
asked by anonymous 13.01.2016 / 19:17

1 answer

2

What happens in your case is that you are always recording everything in the same associative array index.

When you save:

$array_dos_pagamentos["nome"] = $variavel_qualquer;

This means that the index "name" of the_payment_ array receives the value of any_variable. The second time the loop runs it replaces what's inside "name" with a new variable.

To solve your problem, you need to associate the name with the index of your array. In this way:

$array_dos_pagamentos[$variavelnome] = $variavelvalor;

But be careful! If there are two equal names the value of the first one will be replaced by the value of the second one.

    
13.01.2016 / 19:34