php - update array with a value coming from the same array -


i have array structure (this 1 of entries):

[11] => array         (             [id_cred] => cd000000905             [gbv_tot] => 482197.51             [ammesso] => 482197.51             [tipo] => 1             [importo] => 0             [stato] => aperta         ) 

i loop through in foreach set [importo] equal [gbv_tot] or [ammesso] based on conditions. wrote code seems not update value of key [importo].

foreach($creds $cred){     if(($cred['tipo'] == '1')&&($tipo_az == 'conc')){         //sto elaborando un chirografo in una azione concorsuale quindi prendo il nominale         if($cred['stato']=='aperta'){             $gbv_compl = $gbv_compl + $cred['ammesso'];             $cred['importo'] = $cred['ammesso'];         }else{             $cred['importo'] = 0;         }     }else{         //sto elaborando qualcosa d'altro e quindi prendo il gbv         if($cred['stato']=='aperta'){             $gbv_compl = $gbv_compl + $cred['gbv_tot'];                              $cred['importo'] = $cred['gbv_tot'];         }else{             $cred['importo'] = 0;         }     } } 

i think not right way since cannot [importo] updated. missing?

with foreach($creds $cred){ $cred not refer main array. each iteration assign current element $cred. need use key -

foreach($creds $key => $cred){ 

and set value -

$creds[$key]['importo'] = 'whatever value is'; 

in order able directly modify array elements within loop precede $value &. in case value assigned reference.

foreach ($creds &$cred) { 

by using reference can directly update them. -

$cred['importo'] = 0; 

will change value of current element of main array.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -