note 63382 deleted from function.array by bjori

From: Date: Tue, 21 Mar 2006 20:24:19 +0000
Subject: note 63382 deleted from function.array by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-106177@lists.php.net to get a copy of this message
Note Submitter: thomas at example dot com Reason: bad code ---- 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; } ?>

« previous php.notes (#106177) next »