Skip to content

ext/standard: Implement list_filter() #18291

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
77 changes: 77 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6582,6 +6582,83 @@ PHP_FUNCTION(array_filter)
}
/* }}} */

/* {{{ Filters elements from the array via the callback. */
PHP_FUNCTION(list_filter)
{
zval *array;
zval *operand;
zval *key;
zval args[2];
zval retval;
bool have_callback = 0;
zend_long use_type = 0;
zend_string *string_key;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
zend_ulong num_key;

ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_ARRAY(array)
Z_PARAM_OPTIONAL
Z_PARAM_FUNC_OR_NULL(fci, fci_cache)
Z_PARAM_LONG(use_type)
ZEND_PARSE_PARAMETERS_END();

if (zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) {
RETVAL_EMPTY_ARRAY();
return;
}
array_init(return_value);

if (ZEND_FCI_INITIALIZED(fci)) {
have_callback = 1;
fci.retval = &retval;
if (use_type == ARRAY_FILTER_USE_BOTH) {
fci.param_count = 2;
key = &args[1];
} else {
fci.param_count = 1;
key = &args[0];
}
}

ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, string_key, operand) {
if (have_callback) {
if (use_type) {
/* Set up the key */
if (!string_key) {
ZVAL_LONG(key, num_key);
} else {
ZVAL_STR(key, string_key);
}
}
if (use_type != ARRAY_FILTER_USE_KEY) {
ZVAL_COPY_VALUE(&args[0], operand);
}
fci.params = args;

zend_result result = zend_call_function(&fci, &fci_cache);
ZEND_ASSERT(result == SUCCESS);

if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}

if (!php_is_true(&retval)) {
continue;
}
} else if (!zend_is_true(operand)) {
continue;
}

zval new_val;
ZVAL_COPY(&new_val, operand);
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &new_val);
zval_add_ref(operand);
} ZEND_HASH_FOREACH_END();
}
/* }}} */

/* {{{ Internal function to find an array element for a user closure. */
enum php_array_find_result {
PHP_ARRAY_FIND_EXCEPTION = -1,
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,8 @@ function array_reduce(array $array, callable $callback, mixed $initial = null):

function array_filter(array $array, ?callable $callback = null, int $mode = 0): array {}

function list_filter(array $array, ?callable $callback = null, int $mode = 0): array {}

function array_find(array $array, callable $callback): mixed {}

function array_find_key(array $array, callable $callback): mixed {}
Expand Down
6 changes: 5 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions ext/standard/tests/general_functions/list_array.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Test list_filter() function
--FILE--
<?php

$array1 = [1,0,2,3,4];
$array2 = ['a' => 1, 'b' => 2, 'c' => 3];

var_dump(array_is_list($array1));
var_dump(array_is_list(array_filter($array1)));
var_dump(array_is_list(list_filter($array1)));


var_dump(array_is_list($array2));
var_dump(array_is_list(array_filter($array2)));
var_dump(array_is_list(list_filter($array2)));

?>
--EXPECT--
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
Loading