Skip to content

Simplify array_any(), array_all(), array_find(), array_find_key() #17978

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

Merged
merged 1 commit into from
Mar 6, 2025
Merged
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
76 changes: 23 additions & 53 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6591,24 +6591,22 @@ PHP_FUNCTION(array_filter)
/* }}} */

/* {{{ Internal function to find an array element for a user closure. */
static zend_result php_array_find(const HashTable *array, zend_fcall_info fci, zend_fcall_info_cache fci_cache, zval *result_key, zval *result_value, bool negate_condition)
enum php_array_find_result {
PHP_ARRAY_FIND_EXCEPTION = -1,
PHP_ARRAY_FIND_NONE = 0,
PHP_ARRAY_FIND_SOME = 1,
};

static enum php_array_find_result php_array_find(const HashTable *array, zend_fcall_info fci, zend_fcall_info_cache fci_cache, zval *result_key, zval *result_value, bool negate_condition)
{
zend_ulong num_key;
zend_string *str_key;
zval retval;
zval args[2];
zval *operand;

if (result_value != NULL) {
ZVAL_UNDEF(result_value);
}

if (result_key != NULL) {
ZVAL_UNDEF(result_key);
}

if (zend_hash_num_elements(array) == 0) {
return SUCCESS;
return PHP_ARRAY_FIND_NONE;
}

ZEND_ASSERT(ZEND_FCI_INITIALIZED(fci));
Expand All @@ -6631,7 +6629,7 @@ static zend_result php_array_find(const HashTable *array, zend_fcall_info fci, z
ZEND_ASSERT(result == SUCCESS);

if (UNEXPECTED(Z_ISUNDEF(retval))) {
return FAILURE;
return PHP_ARRAY_FIND_EXCEPTION;
}

bool retval_true = zend_is_true(&retval);
Expand All @@ -6649,103 +6647,75 @@ static zend_result php_array_find(const HashTable *array, zend_fcall_info fci, z
ZVAL_COPY(result_key, &args[1]);
}

break;
return PHP_ARRAY_FIND_SOME;
}
} ZEND_HASH_FOREACH_END();

return SUCCESS;
return PHP_ARRAY_FIND_NONE;
}
/* }}} */

/* {{{ Search within an array and returns the first found element value. */
PHP_FUNCTION(array_find)
{
zval *array = NULL;
HashTable *array;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ARRAY(array)
Z_PARAM_ARRAY_HT(array)
Z_PARAM_FUNC(fci, fci_cache)
ZEND_PARSE_PARAMETERS_END();

if (php_array_find(Z_ARR_P(array), fci, fci_cache, NULL, return_value, false) != SUCCESS) {
RETURN_THROWS();
}

if (Z_TYPE_P(return_value) == IS_UNDEF) {
RETURN_NULL();
}
php_array_find(array, fci, fci_cache, NULL, return_value, false);
}
/* }}} */

/* {{{ Search within an array and returns the first found element key. */
PHP_FUNCTION(array_find_key)
{
zval *array = NULL;
HashTable *array;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ARRAY(array)
Z_PARAM_ARRAY_HT(array)
Z_PARAM_FUNC(fci, fci_cache)
ZEND_PARSE_PARAMETERS_END();

if (php_array_find(Z_ARR_P(array), fci, fci_cache, return_value, NULL, false) != SUCCESS) {
RETURN_THROWS();
}

if (Z_TYPE_P(return_value) == IS_UNDEF) {
RETURN_NULL();
}
php_array_find(array, fci, fci_cache, return_value, NULL, false);
}
/* }}} */

/* {{{ Checks if at least one array element satisfies a callback function. */
PHP_FUNCTION(array_any)
{
zval *array = NULL;
HashTable *array;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ARRAY(array)
Z_PARAM_ARRAY_HT(array)
Z_PARAM_FUNC(fci, fci_cache)
ZEND_PARSE_PARAMETERS_END();

if (php_array_find(Z_ARR_P(array), fci, fci_cache, return_value, NULL, false) != SUCCESS) {
RETURN_THROWS();
}

bool retval = !Z_ISUNDEF_P(return_value);
if (Z_TYPE_P(return_value) == IS_STRING) {
zval_ptr_dtor_str(return_value);
}
RETURN_BOOL(retval);
RETURN_BOOL(php_array_find(array, fci, fci_cache, NULL, NULL, false) == PHP_ARRAY_FIND_SOME);
}
/* }}} */

/* {{{ Checks if all array elements satisfy a callback function. */
PHP_FUNCTION(array_all)
{
zval *array = NULL;
HashTable *array;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ARRAY(array)
Z_PARAM_ARRAY_HT(array)
Z_PARAM_FUNC(fci, fci_cache)
ZEND_PARSE_PARAMETERS_END();

if (php_array_find(Z_ARR_P(array), fci, fci_cache, return_value, NULL, true) != SUCCESS) {
RETURN_THROWS();
}

bool retval = Z_ISUNDEF_P(return_value);
if (Z_TYPE_P(return_value) == IS_STRING) {
zval_ptr_dtor_str(return_value);
}
RETURN_BOOL(retval);
RETURN_BOOL(php_array_find(array, fci, fci_cache, NULL, NULL, true) == PHP_ARRAY_FIND_NONE);
}
/* }}} */

Expand Down
Loading