Skip to content

Added array_pick($array) function: gets value of random array item. #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4011,6 +4011,38 @@ PHP_FUNCTION(array_rand)
}
/* }}} */

/* {{{ proto mixed array_pick(array input)
Return value for random entry in the array */
PHP_FUNCTION(array_pick)
{
zval *input;
long randval;
int num_avail, key_type;
zval **data;
HashPosition pos;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &input) == FAILURE) {
return;
}

num_avail = zend_hash_num_elements(Z_ARRVAL_P(input));

/* We can't use zend_hash_index_find() because the array may have string keys or gaps. */
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void **) &data, &pos) != HASH_KEY_NON_EXISTANT) {

randval = php_rand(TSRMLS_C);

if ((double) (randval / (PHP_RAND_MAX + 1.0)) < 1.0 / (double) num_avail) {
RETURN_ZVAL(*data, 1, 0);
}
num_avail--;
zend_hash_move_forward_ex(Z_ARRVAL_P(input), &pos);
}
}
/* }}} */


/* {{{ proto mixed array_sum(array input)
Returns the sum of the array entries */
PHP_FUNCTION(array_sum)
Expand Down
5 changes: 5 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_array_rand, 0, 0, 1)
ZEND_ARG_INFO(0, num_req)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_array_pick, 0)
ZEND_ARG_INFO(0, arg) /* ARRAY_INFO(0, arg, 0) */
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_array_sum, 0)
ZEND_ARG_INFO(0, arg) /* ARRAY_INFO(0, arg, 0) */
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -3322,6 +3326,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(array_flip, arginfo_array_flip)
PHP_FE(array_change_key_case, arginfo_array_change_key_case)
PHP_FE(array_rand, arginfo_array_rand)
PHP_FE(array_pick, arginfo_array_pick)
PHP_FE(array_unique, arginfo_array_unique)
PHP_FE(array_intersect, arginfo_array_intersect)
PHP_FE(array_intersect_key, arginfo_array_intersect_key)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ PHP_FUNCTION(array_pad);
PHP_FUNCTION(array_flip);
PHP_FUNCTION(array_change_key_case);
PHP_FUNCTION(array_rand);
PHP_FUNCTION(array_pick);
PHP_FUNCTION(array_unique);
PHP_FUNCTION(array_intersect);
PHP_FUNCTION(array_intersect_key);
Expand Down