Skip to content

Added "key" parameter to array_reduce callback #14986

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6436,7 +6436,9 @@ PHP_FUNCTION(array_product)
PHP_FUNCTION(array_reduce)
{
zval *input;
zval args[2];
zval args[3];
zend_ulong num_key;
zend_string *string_key;
zval *operand;
zval retval;
zend_fcall_info fci;
Expand Down Expand Up @@ -6468,21 +6470,28 @@ PHP_FUNCTION(array_reduce)
}

fci.retval = &retval;
fci.param_count = 2;
fci.param_count = 3;

ZEND_HASH_FOREACH_VAL(htbl, operand) {
ZEND_HASH_FOREACH_KEY_VAL(htbl, num_key, string_key, operand) {
ZVAL_COPY_VALUE(&args[0], return_value);
ZVAL_COPY(&args[1], operand);
if (!string_key) {
ZVAL_LONG(&args[2], num_key);
} else {
ZVAL_STR_COPY(&args[2], string_key);
}
fci.params = args;

if (zend_call_function(&fci, &fci_cache) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
zval_ptr_dtor(&args[2]);
zval_ptr_dtor(&args[1]);
zval_ptr_dtor(&args[0]);
ZVAL_COPY_VALUE(return_value, &retval);
if (UNEXPECTED(Z_ISREF_P(return_value))) {
zend_unwrap_reference(return_value);
}
} else {
zval_ptr_dtor(&args[2]);
zval_ptr_dtor(&args[1]);
zval_ptr_dtor(&args[0]);
RETURN_NULL();
Expand Down
17 changes: 17 additions & 0 deletions ext/standard/tests/array/array_reduce_key.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test array_reduce() function with callback key
--FILE--
<?php
var_dump(array_reduce(range(3, 5), fn ($acc, $v, $k) => $acc += $k));
var_dump(array_reduce(array_flip(range(3, 5)), fn ($acc, $v, $k) => $acc += $k));
var_dump(array_reduce(range(3, 5), fn ($acc, $v, $k) => $acc += $v + $k));
var_dump(array_reduce(array_flip(['Alfa', 'Bravo']), fn ($acc, $v, $k) => $acc .= $k));
# Check for leaks with non-interned string.
var_dump(array_reduce(array_flip([str_repeat('Charlie', random_int(1, 1)), 'Delta']), fn ($acc, $v, $k) => $acc .= $k));
?>
--EXPECT--
int(3)
int(12)
int(15)
string(9) "AlfaBravo"
string(12) "CharlieDelta"
8 changes: 4 additions & 4 deletions ext/standard/tests/array/array_reduce_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function oneArg($v) {
return $v;
}

function threeArgs($v, $w, $x) {
return $v + $w + $x;
function fourArgs($v, $w, $x, $y) {
return $v + $w + $x + $y;
}

$array = array(1);
Expand All @@ -20,7 +20,7 @@ var_dump(array_reduce($array, "oneArg", 2));

echo "\n--- Testing with a callback with too many parameters ---\n";
try {
var_dump(array_reduce($array, "threeArgs", 2));
var_dump(array_reduce($array, 'fourArgs', 2));
} catch (Throwable $e) {
echo "Exception: " . $e->getMessage() . "\n";
}
Expand All @@ -33,4 +33,4 @@ try {
int(2)

--- Testing with a callback with too many parameters ---
Exception: Too few arguments to function threeArgs(), 2 passed and exactly 3 expected
Exception: Too few arguments to function fourArgs(), 3 passed and exactly 4 expected
Loading