note 63382 added to function.array

From: Date: Tue, 21 Mar 2006 20:05:23 +0000
Subject: note 63382 added to function.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-106174@lists.php.net to get a copy of this message
Here is a tiny function which recursively retrieves an array's size in bytes. It should be quite handy for stopping user-supplied data increasing an array beyond a preset limit. The function could be extended in order to weigh in associative array keys as well. <?php define ( "ARRAY_SIZE_INT32", 4 ); // float size is probably platform dependent so you may want to adjust accordingly define ( "ARRAY_SIZE_DOUBLE", 8 ); // Returns an arbitrary variable's size in bytes function var_size ( &$var ) { if ( is_array ( $var ) ) return array_size ( $var ); if ( is_string ( $var ) ) return strlen ( $var ); if ( is_integer ( $var ) ) return ARRAY_SIZE_INT32; if ( is_double ( $var ) ) return ARRAY_SIZE_DOUBLE; if ( is_object ( $var ) ) return array_size ( get_object_vars ( $var ) ); } // Returns an array's size in bytes function array_size ( &$array ) { $size = 0; if ( count ( $array ) > 0 ) foreach ( $array as $elm ) $size += var_size ( $elm ); return $size; } ?> ---- Server IP: 217.13.201.10 Probable Submitter: 213.54.177.108 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.array.php Edit -- http://master.php.net/note/edit/63382 Del: integrated -- http://master.php.net/note/delete/63382/integrated Del: useless -- http://master.php.net/note/delete/63382/useless Del: bad code -- http://master.php.net/note/delete/63382/bad+code Del: spam -- http://master.php.net/note/delete/63382/spam Del: non-english -- http://master.php.net/note/delete/63382/non-english Del: in docs -- http://master.php.net/note/delete/63382/in+docs Del: other reasons-- http://master.php.net/note/delete/63382 Reject -- http://master.php.net/note/reject/63382 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106174) next »