我有这个例子。 JSON
// my json
$json = {"sample_rate":44100,"samples_per_pixel":4410,"bits":8,"length":1847,"data":[0,-46,37,-18,19,...
如果我将数据中的对象除以150,就像这样:
// decoding the string to objects & arrays
$x = json_decode($json);
// applying a function on each value of the array
$x->data = array_map(
function($a)
{
if( $a >= 0 ) return $a/150;
else return null;
},
$x->data
);
$x->data = array_filter($x->data);
$x->data = array_values($x->data);
$json = json_encode($x);
结果是这样的:
{"sample_rate":44100,"samples_per_pixel":4410,"bits":8,"length":1847,"data":[0,0.246666666666667,0.126666666666667,...
所以我的问题是,如何限制数组中每个对象的MAX 2小数?