Penwell Posted February 22, 2020 Share Posted February 22, 2020 I have this php file which i use to get downloaded and uploaded data usage from my splynx radius server over API which outputs it as bytes making a very long string which i want to shorten into Mbps and from the arrays that i get from splynx server i would like help to convert the array "in_bytes" and "out_bytes" to MB. This is my whole code below. currently the output is in bytes and i need it to be in MB <?php $api_url = 'https://xxx/'; // Splynx URL $admin_login = "xxx"; // administrator login $admin_password = "xxx"; // administrator password $api = new SplynxAPI($api_url); $api->setVersion(SplynxApi::API_VERSION_2); $isAuthorized = $api->login([ 'auth_type' => SplynxApi::AUTH_TYPE_ADMIN, 'login' => $admin_login, 'password' => $admin_password, ]); if (!$isAuthorized) { exit("Authorization failed!\n"); } $customerApiUrl_online = "admin/customers/customers-online"; $customers_params = [ 'main_attributes' => [ 'status' => ['IN', ['active', 'blocked']] ]]; $result_online = $api->api_call_get($customerApiUrl_online); $customers_online = $api->response; ?> <table class="table table-transparent"> <thead> <tr> <th>DOWNOAD</th> <th>UPLOAD</th> </tr> </thead> <tbody> <?php foreach($customers_online as $item) :?> <tr> <td><?php echo $item['in_bytes']; ?></td> <td><?php echo $item['out_bytes']; ?></td> </tr> <?php endforeach;?> </tbody> </table> Quote Link to comment https://forums.phpfreaks.com/topic/310107-convert-an-array-bytes-to-mb-php/ Share on other sites More sharing options...
gw1500se Posted February 22, 2020 Share Posted February 22, 2020 Simply divide it by 1000000 then round to get the desired precision. You may need to use intval to make the string an integer first. Quote Link to comment https://forums.phpfreaks.com/topic/310107-convert-an-array-bytes-to-mb-php/#findComment-1574777 Share on other sites More sharing options...
Penwell Posted February 22, 2020 Author Share Posted February 22, 2020 13 minutes ago, gw1500se said: Simply divide it by 1000000 then round to get the desired precision. You may need to use intval to make the string an integer first. Can you please show me how to do this on my code. Im still new with PHP and im not sure how to apply this on my code above Quote Link to comment https://forums.phpfreaks.com/topic/310107-convert-an-array-bytes-to-mb-php/#findComment-1574778 Share on other sites More sharing options...
gw1500se Posted February 22, 2020 Share Posted February 22, 2020 (edited) round(floatval($item['in_bytes'])/1000000,2); I used 'floatval' instead of 'intval' since that is what 'round' wants. The 2 gives 2 decimal places but you can use what you want. Edited February 22, 2020 by gw1500se 1 Quote Link to comment https://forums.phpfreaks.com/topic/310107-convert-an-array-bytes-to-mb-php/#findComment-1574780 Share on other sites More sharing options...
Penwell Posted February 22, 2020 Author Share Posted February 22, 2020 16 minutes ago, gw1500se said: round(floatval($item['in_bytes'])/1000000,2); I used 'floatval' instead of 'intval' since that is what 'round' wants. The 2 gives 2 decimal places but you can use what you want. Thanks a lot this works great now Quote Link to comment https://forums.phpfreaks.com/topic/310107-convert-an-array-bytes-to-mb-php/#findComment-1574781 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.