Skip to content
Open
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
12 changes: 11 additions & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6453,7 +6453,7 @@ PHP_FUNCTION(array_filter)
zval args[2];
zval retval;
bool have_callback = 0;
zend_long use_type = 0;
zend_long use_type = ARRAY_FILTER_USE_VALUE;
zend_string *string_key;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fci_cache;
Expand All @@ -6466,6 +6466,16 @@ PHP_FUNCTION(array_filter)
Z_PARAM_LONG(use_type)
ZEND_PARSE_PARAMETERS_END();

switch (use_type) {
case ARRAY_FILTER_USE_VALUE:
case ARRAY_FILTER_USE_BOTH:
case ARRAY_FILTER_USE_KEY:
break;
default:
zend_argument_value_error(3, "must be a valid mode");
RETURN_THROWS();
}

if (zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) {
RETVAL_EMPTY_ARRAY();
return;
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 @@ -59,6 +59,7 @@ PHPAPI bool php_array_pick_keys(php_random_algo_with_state engine, zval *input,
#define PHP_COUNT_NORMAL 0
#define PHP_COUNT_RECURSIVE 1

#define ARRAY_FILTER_USE_VALUE 0
#define ARRAY_FILTER_USE_BOTH 1
#define ARRAY_FILTER_USE_KEY 2

Expand Down
16 changes: 16 additions & 0 deletions ext/standard/tests/array/array_filter_invalid_mode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Test array_filter() function : usage variations - mode exception
--FILE--
<?php

try {
var_dump(array_filter([], mode: 999));
} catch (Throwable $e) {
echo $e::class . ': '.$e->getMessage(), "\n";
}

echo "Done"
?>
--EXPECT--
ValueError: array_filter(): Argument #3 ($mode) must be a valid mode
Done
Loading