note 102933 deleted from function.sizeof by danbrown
| From: | danbrown@php.net | Date: | Tue, 15 Mar 2011 15:13:55 +0000 |
| Subject: | note 102933 deleted from function.sizeof by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177901@lists.php.net to get a copy of this message | ||
Note Submitter: noreply at DeveloperER dot com
----
sizeof() and count() return the NUMBER of elements in the array. Here is a function that will return
the totality of sizes (in bytes) of the values in an array (similar to strlen() but over all array
contents):
function ARRAY_SIZE($a,$n2s=false)
{ // Purpose: Return the combined byte size of the array's contents or false if $a is not an
array.
// Usage: ARRAY_SIZE($array_name); ARRAY_SIZE($array_name,true);
// Notes: If $n2s is true (not false), numbers will be converted to strings before calculating
sizes:
// e.g. 1000 = 4 characters (4 bytes) instead of the default, 1000 = b(0000001111101000) = 2
bytes
// Copyright (c) Peter Kionga-Kamau http://www.DeveloperER.com
// License: Free for unrestricted use under two conditions:
// 1. NOT FOR SALE! You may USE this function for any purpose (including using it in products that
you sell), however:
// YOU MAY NOT SELL THIS FUNCTION even if it is bundled with other functions.
// 2. This comment and copyright notice must not be changed at any time.
// -------------------------------------------
if(!is_array($a))return false;
$s = 0;
foreach($a as $k=>$v) $s+=is_array($v)? ARRAY_SIZE($v):(!$n2s&&is_numeric($v)?
ceil(strlen(decbin($v))/8):strlen($v));
return $s;
}