It's crazy to solve the confusion of array assignment in php ......
The main idea is as follows:
$update=['f1'=>121.231,'f2'=>2312.12, 'f3'=>31.1231,'f4'=>213.12,'fv'=>3189.21321];$month=['m1'=>123.1289,'m2'=>123.23198, 'm3'=>23.21,'m4'=>138.21,'mv'=>1231.81];foreach($update as &$v){$v=round($v,4);}foreach($month as $k=>$v){$update[$k]=round($v,4);}
Please read the above code first. should this be correct? If you have any questions, please point out that I am crazy, and I don't have to look at the next big guy ......
The core is to merge the two arrays together (the other implementation methods will not talk about, just use this), and retain four decimal places
If the above code is correct, see the debugging below.
-------------------------- DEBUG -------------------------
In actual operation, I found that the fv field still retains a large number of decimal places. In short, the round operation was not executed, and I began to doubt my life.
In the code, PR (print_r) looks at the data and finds that after the update foreach, it is still normal, but it is not normal after entering the month loop.
For the compiled debugging code, see:
If ($ debug_flag) {echo ('raw data'); BR (); PR ($ update);} foreach ($ update as $ k => & $ v) {$ v = round ($ v, 4);} // I really don't understand why the last fv of update is not rounded up? If ($ debug_flag) {echo ('data after the update foreach loop '); BR (); PR ($ update); echo ('cycle into month'); BR (); echo ('month original data'); BR (); PR ($ month);} foreach ($ month as $ k => $ v) {if ($ debug_flag) {echo ('In month'); BR (); PR ($ update); PR ($ k); BR ();} $ update [$ k] = round ($ v, 4); if ($ debug_flag) {PR ($ update); echo ('month data in a loop '); BR () ;}} if ($ debug_flag) {echo ('data after month looping '); BR (); PR ($ update); PR ($ month ); die ();}
For the debugging results, see:
Original data Array ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.85900353103) data Array after update foreach loop ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.859) enter the month loop month original data Array ([m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325 [m4] => 602.0825 [mv] => 600.85900353103) in monthArray ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 0) m0Array ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 0 [m0] => 0) in monthArray ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 596.4 [m0] => 0) m1Array ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 596.4 [m0] => 0 [m1] => 596.4) in monthArray ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 604.5 [m0] => 0 [m1] => 596.4) m2Array ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 604.5 [m0] => 0 [m1] => 596.4 [m2] => 604.5) in monthArray ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.3325 [m0] => 0 [m1] => 596.4 [m2] => 604.5) m3Array ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.3325 [m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325) in monthArray ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 602.0825 [m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325) m4Array ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 602.0825 [m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325 [m4] => 602.0825) in monthArray ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.85900353103 [m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325 [m4] => 602.0825) mvArray ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.85900353103 [m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325 [m4] => 602.0825 [mv] => 600.859) data Array ([f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.85900353103 [m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325 [m4] => 602.0825 [mv] => 600.859) array ([m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325 [m4] => 602.0825 [mv] => 600.85900353103)
If you have the patience to read the data, you will find that the data has started to change in the month loop ......
Is there anything unknown about foreach or $ array [$ key] = $ value in an array?
Helper!
Reply to discussion (solution)
By the way, the code is changed after debugging.
$update=array_merge($update,$month,$year);foreach($update as &$v){$v=round($v,4);}
This data is normal, and it is also brief + CLEAR + no error!
But what's wrong with the original code?
$update = array ( 'f1' => 596.4, 'f2' => 604.5, 'f3' => 600.3325, 'f4' => 602.0825, 'fv' => 600.85900353103,);$month = array ( 'm0' => 0, 'm1' => 596.4, 'm2' => 604.5, 'm3' => 600.3325, 'm4' => 602.0825, 'mv' => 600.85900353103,);foreach($update as $k=>&$v){ $v = round($v, 4);}unset($v);foreach($month as $k=>$v){ $update[$k] = round($v, 4);}print_r($update);
Array( [f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.859 [m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325 [m4] => 602.0825 [mv] => 600.859)
Do you see the difference? I used an unset ($ v );
In fact, this is a reference fault. The two groups of data you tested are exactly the same. if you change $ month ['mmv '] to 0, you will immediately find the problem.
$update = array ( 'f1' => 596.4, 'f2' => 604.5, 'f3' => 600.3325, 'f4' => 602.0825, 'fv' => 600.85900353103,);$month = array ( 'm0' => 0, 'm1' => 596.4, 'm2' => 604.5, 'm3' => 600.3325, 'm4' => 602.0825, 'mv' => 600.85900353103,);foreach($update as $k=>&$v){ $v = round($v, 4);}unset($v);foreach($month as $k=>$v){ $update[$k] = round($v, 4);}print_r($update);
Array( [f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.859 [m0] => 0 [m1] => 596.4 [m2] => 604.5 [m3] => 600.3325 [m4] => 602.0825 [mv] => 600.859)
Do you see the difference? I used an unset ($ v );
In fact, this is a reference fault. The two groups of data you tested are exactly the same. if you change $ month ['mmv '] to 0, you will immediately find the problem.
Oh, right ...... I forgot to reference this issue ...... Rarely used ......
Thank you!