cvs: php4 / /ext/standard/

From: Date: Tue, 09 May 2000 19:27:00 +0000
Subject: cvs: php4 / /ext/standard/
Groups: php.version4 
Request: Send a blank email to php-version4+get-14685@lists.php.net to get a copy of this message
andrei Tue May 9 21:27:00 2000 EDT Modified files: /php4 TODO /php4/ext/standard array.c basic_functions.c php_array.h Log: @- Added array_rand() function. (Andrei) Index: php4/TODO diff -u php4/TODO:1.78 php4/TODO:1.79 --- php4/TODO:1.78 Thu May 4 22:04:38 2000 +++ php4/TODO Tue May 9 21:27:00 2000 @@ -63,7 +63,7 @@ ext/standard ------------ * add a version number to data serialized via serialize(). - * array_add(), array_rand(), array_uniq(). (Andrei) + * array_add(), array_uniq(). (Andrei) * implement pathinfo() (what the heck is it?) * possibly modify parsing of GPC data to automatically create arrays if variable name is seen more than once. Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.40 php4/ext/standard/array.c:1.41 --- php4/ext/standard/array.c:1.40 Fri Apr 28 16:01:08 2000 +++ php4/ext/standard/array.c Tue May 9 21:27:00 2000 @@ -40,6 +40,7 @@ #include "php_array.h" #include "basic_functions.h" #include "php_string.h" +#include "php_rand.h" #ifdef ZTS int array_globals_id; @@ -2220,6 +2221,84 @@ efree(arrays); efree(args); RETURN_TRUE; +} +/* }}} */ + + +/* {{{ proto mixed array_rand(array input [, int num_req ]) + Return key/keys for random entry/entries in the array */ +PHP_FUNCTION(array_rand) +{ + zval **input, **num_req; + long randval; + int num_req_val, num_avail, key_type; + char *string_key; + ulong num_key; + + if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 || + zend_get_parameters_ex(ZEND_NUM_ARGS(), &input, &num_req) == FAILURE) { + WRONG_PARAM_COUNT; + } + + if (Z_TYPE_PP(input) != IS_ARRAY) { + zend_error(E_WARNING, "Argument to %s() has to be an array", + get_active_function_name()); + return; + } + + num_avail = zend_hash_num_elements(Z_ARRVAL_PP(input)); + + if (ZEND_NUM_ARGS() > 1) { + convert_to_long_ex(num_req); + num_req_val = Z_LVAL_PP(num_req); + if (num_req_val <= 0 || num_req_val > num_avail) { + zend_error(E_WARNING, "Second argument to %s() has to be between 1 and the number of elements in the array", get_active_function_name()); + return; + } + } else + num_req_val = 1; + + /* Make the return value an array only if we need to pass back more than one + result. */ + if (num_req_val > 1) + array_init(return_value); + + /* We can't use zend_hash_index_find() because the array may have string keys or gaps. */ + zend_hash_internal_pointer_reset(Z_ARRVAL_PP(input)); + while (num_req_val && (key_type = zend_hash_get_current_key(Z_ARRVAL_PP(input), &string_key, &num_key)) != HASH_KEY_NON_EXISTANT) { + +#ifdef HAVE_LRAND48 + randval = lrand48(); +#else +#ifdef HAVE_RANDOM + randval = random(); +#else + randval = rand(); +#endif +#endif + + if ((double)(randval/(PHP_RAND_MAX+1.0)) < (double)num_req_val/(double)num_avail) { + /* If we are returning a single result, just do it. */ + if (Z_TYPE_P(return_value) != IS_ARRAY) { + if (key_type == HASH_KEY_IS_STRING) { + RETURN_STRING(string_key, 0); + } else { + RETURN_LONG(num_key); + } + } else { + /* Append the result to the return value. */ + if (key_type == HASH_KEY_IS_STRING) + add_next_index_string(return_value, string_key, 0); + else + add_next_index_long(return_value, num_key); + } + num_req_val--; + } else if (key_type == HASH_KEY_IS_STRING) + efree(string_key); + + num_avail--; + zend_hash_move_forward(Z_ARRVAL_PP(input)); + } } /* }}} */ Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.188 php4/ext/standard/basic_functions.c:1.189 --- php4/ext/standard/basic_functions.c:1.188 Sun May 7 02:10:32 2000 +++ php4/ext/standard/basic_functions.c Tue May 9 21:27:00 2000 @@ -487,6 +487,7 @@ PHP_FE(array_reverse, NULL) PHP_FE(array_pad, NULL) PHP_FE(array_flip, NULL) + PHP_FE(array_rand, NULL) /* aliases from array.c */ PHP_FALIAS(pos, current, first_arg_force_ref) PHP_FALIAS(sizeof, count, first_arg_allow_ref) Index: php4/ext/standard/php_array.h diff -u php4/ext/standard/php_array.h:1.7 php4/ext/standard/php_array.h:1.8 --- php4/ext/standard/php_array.h:1.7 Wed Apr 12 21:39:02 2000 +++ php4/ext/standard/php_array.h Tue May 9 21:27:00 2000 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_array.h,v 1.7 2000/04/12 19:39:02 andrei Exp $ */ +/* $Id: php_array.h,v 1.8 2000/05/09 19:27:00 andrei Exp $ */ #ifndef _PHP_ARRAY_H #define _PHP_ARRAY_H @@ -67,6 +67,7 @@ PHP_FUNCTION(array_reverse); PHP_FUNCTION(array_pad); PHP_FUNCTION(array_flip); +PHP_FUNCTION(array_rand); HashTable* _phpi_splice(HashTable *, int, int, zval ***, int, HashTable **); int multisort_compare(const void *a, const void *b);

« previous php.version4 (#14685) next »