note 102933 added to function.sizeof

From: Date: Tue, 15 Mar 2011 15:11:26 +0000
Subject: note 102933 added to function.sizeof
Groups: php.notes 
Request: Send a blank email to php-notes+get-177900@lists.php.net to get a copy of this message
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; } ---- Server IP: 69.147.83.197 Probable Submitter: 71.63.5.242 ---- Manual Page -- http://www.php.net/manual/en/function.sizeof.php Edit -- https://master.php.net/note/edit/102933 Del: integrated -- https://master.php.net/note/delete/102933/integrated Del: useless -- https://master.php.net/note/delete/102933/useless Del: bad code -- https://master.php.net/note/delete/102933/bad+code Del: spam -- https://master.php.net/note/delete/102933/spam Del: non-english -- https://master.php.net/note/delete/102933/non-english Del: in docs -- https://master.php.net/note/delete/102933/in+docs Del: other reasons-- https://master.php.net/note/delete/102933 Reject -- https://master.php.net/note/reject/102933 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177900) next »