2

I have this eg. of JSON

// my json
$json = {"sample_rate":44100,"samples_per_pixel":4410,"bits":8,"length":1847,"data":[0,-46,37,-18,19,...

If I divide objects in data by 150 like this:

// 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);

The result is something like this:

{"sample_rate":44100,"samples_per_pixel":4410,"bits":8,"length":1847,"data":[0,0.246666666666667,0.126666666666667,...

So my question is, how can I limit at MAX 2 decimals each objects in array?

0 Answers0