Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Created July 17, 2015 13:38
Show Gist options
  • Save kirilkirkov/0444d3fd95ef30facac3 to your computer and use it in GitHub Desktop.
Save kirilkirkov/0444d3fd95ef30facac3 to your computer and use it in GitHub Desktop.
Return a size in bytes,kilobytes,megabytes and gigabytes for given array or string. Can be used if you want to check performance for some script.
<?php
function array_size($arr) {
$byte = 0;
foreach ($arr as $key => $val) {
$byte += is_array($val) ? array_size($val) : mb_strlen($val);
}
$kb = number_format($byte / 1024, 4);
$mb = number_format($byte / 1048576, 4);
$gb = number_format($byte / 1073741824, 4);
$result = array('Bytes: ' => $byte, 'Kilobytes: ' => $kb, 'Megabytes: ' => $mb, 'Gigabytes: ' => $gb);
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment