diff --git a/Zend/zend_hash.h b/Zend/zend_hash.h index 5726c8a919..c462de2850 100644 --- a/Zend/zend_hash.h +++ b/Zend/zend_hash.h @@ -1087,6 +1087,10 @@ static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, _ZEND_HASH_FOREACH_VAL(ht); \ _val = _z; +#define ZEND_HASH_FOREACH_VAL_FROM(ht, _val, _from) \ + ZEND_HASH_FOREACH_FROM(ht, 0, _from); \ + _val = _z; + #define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \ _ZEND_HASH_REVERSE_FOREACH_VAL(ht); \ _val = _z; diff --git a/ext/standard/array.c b/ext/standard/array.c index 46c2c882b8..918aa54263 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -6394,6 +6394,114 @@ PHP_FUNCTION(array_map) } /* }}} */ +/* {{{ Groups elements from the array via the callback. */ +PHP_FUNCTION(array_group) +{ + zval *array; + zend_fcall_info fci; + zend_fcall_info_cache fci_cache = empty_fcall_info_cache; + + zval args[2]; + zval *curr_val; + zval chunk; + zval retval; + bool consecutive_pairs = 0; + + ZEND_PARSE_PARAMETERS_START(2, 3) + Z_PARAM_ARRAY(array) + Z_PARAM_FUNC(fci, fci_cache) + Z_PARAM_OPTIONAL + Z_PARAM_BOOL(consecutive_pairs) + ZEND_PARSE_PARAMETERS_END(); + + if (zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) { + RETVAL_EMPTY_ARRAY(); + return; + } + + array_init(return_value); + fci.retval = &retval; + + if (consecutive_pairs) { + zval *prev_val; + + fci.param_count = 2; + + // The array is guaranteed to have at least one element. + prev_val = ZEND_HASH_ELEMENT(Z_ARRVAL_P(array), 0); + + // Generate the initial group. + array_init(&chunk); + zend_hash_next_index_insert_new(Z_ARRVAL_P(&chunk), prev_val); + + ZEND_HASH_FOREACH_VAL_FROM(Z_ARRVAL_P(array), curr_val, 1) { + ZVAL_COPY(&args[0], prev_val); + ZVAL_COPY(&args[1], curr_val); + fci.params = args; + + if (zend_call_function(&fci, &fci_cache) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { + int retval_true; + + zval_ptr_dtor(&args[1]); + zval_ptr_dtor(&args[0]); + + retval_true = zend_is_true(&retval); + + zval_ptr_dtor(&retval); + + // Perform grouping - add the current group and create a new one. + if (!retval_true) { + zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &chunk); + array_init(&chunk); + } + + zend_hash_next_index_insert_new(Z_ARRVAL_P(&chunk), curr_val); + + prev_val = curr_val; + } else { + zval_ptr_dtor(&args[1]); + zval_ptr_dtor(&args[0]); + RETURN_NULL(); + } + } ZEND_HASH_FOREACH_END(); + + // Add the last group. + zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &chunk); + } else { + zval *group; + + fci.param_count = 1; + + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(array), curr_val) { + ZVAL_COPY(&args[0], curr_val); + fci.params = args; + + if (zend_call_function(&fci, &fci_cache) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { + if (Z_TYPE_P(&retval) != IS_STRING) { + php_error_docref(NULL, E_WARNING, "Argument #1 must be string, %s given", zend_zval_value_name(&retval)); + RETVAL_EMPTY_ARRAY(); + return; + } + group = zend_hash_find(Z_ARRVAL_P(return_value), Z_STR_P(&retval)); + if (group == NULL) { + array_init(&chunk); + group = &chunk; + zend_hash_add_new(Z_ARRVAL_P(return_value), Z_STR_P(&retval), group); + } + + zend_hash_next_index_insert(Z_ARRVAL_P(group), curr_val); + zval_ptr_dtor(&args[0]); + zval_ptr_dtor(&retval); + + } else { + zval_ptr_dtor(&args[0]); + RETURN_NULL(); + } + } ZEND_HASH_FOREACH_END(); + } +} +/* }}} */ + /* {{{ Checks if the given key or index exists in the array */ PHP_FUNCTION(array_key_exists) { diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index effb05ff9f..395ec67fc0 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -1870,6 +1870,8 @@ function array_filter(array $array, ?callable $callback = null, int $mode = 0): function array_map(?callable $callback, array $array, array ...$arrays): array {} +function array_group(array $array, callable $callback, bool $consecutive_pairs = false): array {} + /** * @param string|int $key * @compile-time-eval diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 5612ee2186..dce035fd6b 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -342,6 +342,12 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_map, 0, 2, IS_ARRAY, 0) ZEND_ARG_VARIADIC_TYPE_INFO(0, arrays, IS_ARRAY, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_group, 0, 2, IS_ARRAY, 0) + ZEND_ARG_TYPE_INFO(0, array, IS_ARRAY, 0) + ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, consecutive_pairs, _IS_BOOL, 0, "false") +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_key_exists, 0, 2, _IS_BOOL, 0) ZEND_ARG_INFO(0, key) ZEND_ARG_TYPE_INFO(0, array, IS_ARRAY, 0) @@ -2292,6 +2298,7 @@ ZEND_FUNCTION(array_product); ZEND_FUNCTION(array_reduce); ZEND_FUNCTION(array_filter); ZEND_FUNCTION(array_map); +ZEND_FUNCTION(array_group); ZEND_FUNCTION(array_key_exists); ZEND_FUNCTION(array_chunk); ZEND_FUNCTION(array_combine); @@ -2915,6 +2922,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(array_reduce, arginfo_array_reduce) ZEND_FE(array_filter, arginfo_array_filter) ZEND_FE(array_map, arginfo_array_map) + ZEND_FE(array_group, arginfo_array_group) ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(array_key_exists, arginfo_array_key_exists) ZEND_FALIAS(key_exists, array_key_exists, arginfo_key_exists) ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(array_chunk, arginfo_array_chunk) diff --git a/ext/standard/tests/array/array_group_basic.phpt b/ext/standard/tests/array/array_group_basic.phpt new file mode 100644 index 0000000000..582c4ff22a --- /dev/null +++ b/ext/standard/tests/array/array_group_basic.phpt @@ -0,0 +1,41 @@ +--TEST-- +Test array_group() function : basic functionality +--FILE-- + +--EXPECT-- +*** Testing array_group() : basic functionality *** +-- With an array of three elements -- +array(2) { + ["3"]=> + array(2) { + [0]=> + string(3) "one" + [1]=> + string(3) "two" + } + ["5"]=> + array(1) { + [0]=> + string(5) "three" + } +} +-- With an empty array -- +array(0) { +} +Done diff --git a/ext/standard/tests/array/array_group_consecutive_pairs.phpt b/ext/standard/tests/array/array_group_consecutive_pairs.phpt new file mode 100644 index 0000000000..a8193f3212 --- /dev/null +++ b/ext/standard/tests/array/array_group_consecutive_pairs.phpt @@ -0,0 +1,115 @@ +--TEST-- +Test array_group() function : basic functionality (consecutive pairs) +--FILE-- +name == $b->name; +} + +$arr1 = array(1, 2, 3); + +echo "-- With an integer array for < --\n"; +var_dump( array_group($arr1, 'less_than', true) ); + +echo "-- With an integer array for == --\n"; +var_dump( array_group($arr1, 'equal', true) ); + +echo "-- With an empty array for == --\n"; +var_dump( array_group(array(), 'equal', true) ); + +echo "-- With a singleton integer array for == --\n"; +var_dump( array_group(array(1), 'equal', true) ); + +$obj1 = (object)array('id'=>3,'name'=>'foo'); +$obj2 = (object)array('id'=>4,'name'=>'foo'); +$obj3 = (object)array('id'=>5,'name'=>'baz'); + +echo "-- With an integer array of objects for == --\n"; +var_dump( array_group(array($obj1, $obj2, $obj3), 'equal_obj', true) ); + +echo "Done"; +?> +--EXPECT-- +*** Testing array_group() : basic functionality (consecutive pairs) *** +-- With an integer array for < -- +array(1) { + [0]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } +} +-- With an integer array for == -- +array(3) { + [0]=> + array(1) { + [0]=> + int(1) + } + [1]=> + array(1) { + [0]=> + int(2) + } + [2]=> + array(1) { + [0]=> + int(3) + } +} +-- With an empty array for == -- +array(0) { +} +-- With a singleton integer array for == -- +array(1) { + [0]=> + array(1) { + [0]=> + int(1) + } +} +-- With an integer array of objects for == -- +array(2) { + [0]=> + array(2) { + [0]=> + object(stdClass)#1 (2) { + ["id"]=> + int(3) + ["name"]=> + string(3) "foo" + } + [1]=> + object(stdClass)#2 (2) { + ["id"]=> + int(4) + ["name"]=> + string(3) "foo" + } + } + [1]=> + array(1) { + [0]=> + object(stdClass)#3 (2) { + ["id"]=> + int(5) + ["name"]=> + string(3) "baz" + } + } +} +Done diff --git a/ext/standard/tests/array/array_group_incr_subseqs.phpt b/ext/standard/tests/array/array_group_incr_subseqs.phpt new file mode 100644 index 0000000000..245387862f --- /dev/null +++ b/ext/standard/tests/array/array_group_incr_subseqs.phpt @@ -0,0 +1,53 @@ +--TEST-- +Test array_group() function : increasing subsequences +--FILE-- + +--EXPECT-- +*** Testing array_group() : increasing subsequences *** +array(4) { + [0]=> + array(4) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(2) + [3]=> + int(3) + } + [1]=> + array(2) { + [0]=> + int(1) + [1]=> + int(2) + } + [2]=> + array(3) { + [0]=> + int(0) + [1]=> + int(4) + [2]=> + int(5) + } + [3]=> + array(1) { + [0]=> + int(2) + } +} +Done