Rumah >pembangunan bahagian belakang >tutorial php >php中数组赋值混乱求解
真抓狂了……
大意如下:
$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); }
if($debug_flag){ echo('原始数据');BR(); PR($update); } foreach($update as $k=>&$v){ $v=round($v,4); } //实在没有整明白为什么update的最后一个fv不进行四舍五入呢? if($debug_flag){ echo('update foreach 循环后数据');BR(); PR($update); echo('进入month循环');BR(); echo('month原始数据');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 循环中数据');BR(); } } if($debug_flag){ echo('month循环后数据');BR(); PR($update); PR($month); die(); }
原始数据Array( [f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.85900353103)update foreach 循环后数据Array( [f1] => 596.4 [f2] => 604.5 [f3] => 600.3325 [f4] => 602.0825 [fv] => 600.859)进入month循环month原始数据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)month 循环中数据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)month 循环中数据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)month 循环中数据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)month 循环中数据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)month 循环中数据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)month 循环中数据month循环后数据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)
调试到这里顺便就把代码改了
$update=array_merge($update,$month,$year); foreach($update as &$v){ $v=round($v,4); }
$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)看到区别了吗?我多用了个 unset($v);
$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)看到区别了吗?我多用了个 unset($v);