add to array after loop

Asked

Viewed 195 times

0

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

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

inside a loop I tried : $array_dos_pagamentos[nome] = $variavelnome; and another loop $array_dos_pagamentos[valor] = $variavelvalor;

the problem is that the Array ( [nome] => Susana laginha, [valor] => 84 ) or just a record.. 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 
) 
  • try $array_dos_pagamentos = array('nome' => $variavelnome, 'valor' => $variavelvalor);

  • remembering that if the question answered your question, mark the question as answered.

1 answer

2


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

When you record:

$array_dos_pagamentos["nome"] = $variavel_qualquer;

This means that the index "name" of the payments array receives the value of any variable_any. The second time the loop rotates it replaces what is inside "name" with a new variable_any.

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

$array_dos_pagamentos[$variavelnome] = $variavelvalor;

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

Browser other questions tagged

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