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: 10 additions & 2 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6511,13 +6511,19 @@ PHP_FUNCTION(array_filter)
Z_PARAM_LONG(use_type)
ZEND_PARSE_PARAMETERS_END();

bool pack_result = use_type & ARRAY_FILTER_PACK_RESULT;
use_type &= ~ARRAY_FILTER_PACK_RESULT;

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 one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH");
zend_argument_value_error(
3,
"must be one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH (optionally combined with ARRAY_FILTER_PACK_RESULT)"
);
RETURN_THROWS();
}

Expand Down Expand Up @@ -6567,7 +6573,9 @@ PHP_FUNCTION(array_filter)
continue;
}

if (string_key) {
if (pack_result) {
operand = zend_hash_next_index_insert(Z_ARRVAL_P(return_value), operand);
} else if (string_key) {
operand = zend_hash_add_new(Z_ARRVAL_P(return_value), string_key, operand);
} else {
operand = zend_hash_index_add_new(Z_ARRVAL_P(return_value), num_key, operand);
Expand Down
5 changes: 5 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ enum SortDirection {
* @cvalue ARRAY_FILTER_USE_KEY
*/
const ARRAY_FILTER_USE_KEY = UNKNOWN;
/**
* @var int
* @cvalue ARRAY_FILTER_PACK_RESULT
*/
const ARRAY_FILTER_PACK_RESULT = UNKNOWN;

/* assert.c */

Expand Down
3 changes: 2 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.

8 changes: 4 additions & 4 deletions ext/standard/basic_functions_decl.h

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

1 change: 1 addition & 0 deletions ext/standard/php_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ PHPAPI bool php_array_pick_keys(php_random_algo_with_state engine, zval *input,
#define ARRAY_FILTER_USE_VALUE 0
#define ARRAY_FILTER_USE_BOTH 1
#define ARRAY_FILTER_USE_KEY 2
#define ARRAY_FILTER_PACK_RESULT 4

extern PHPAPI zend_class_entry *sort_direction_ce;

Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/array/array_filter_invalid_mode.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ try {
echo "Done"
?>
--EXPECT--
ValueError: array_filter(): Argument #3 ($mode) must be one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH
ValueError: array_filter(): Argument #3 ($mode) must be one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH (optionally combined with ARRAY_FILTER_PACK_RESULT)
Done
121 changes: 121 additions & 0 deletions ext/standard/tests/array/array_filter_pack.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
--TEST--
array_filter() tests with ARRAY_FILTER_PACK_RESULT
--FILE--
<?php

$array1 = [false, null, true, '', 'foo', 0, 123];
var_dump( array_filter( $array1 ) );
var_dump( array_filter( $array1, null, ARRAY_FILTER_PACK_RESULT ) );

$array2 = [
'index 0 means false key',
'' => 'false key empty string',
false,
];
// to filter based on a truthy key requires an explicit callback
var_dump( array_filter( $array2, fn ($v) => $v, ARRAY_FILTER_USE_KEY ) );
var_dump( array_filter( $array2, fn ($v) => $v, ARRAY_FILTER_USE_KEY | ARRAY_FILTER_PACK_RESULT ) );

$array3 = [
'key1' => true,
'key2' => false,
'key3' => 123,
];
function filter( $value, $key ) {
$value = var_export( $value, true );
echo 'Filter: $key => $value\n';
return true;
}
var_dump( array_filter( $array3, filter(...), ARRAY_FILTER_USE_BOTH ) );
var_dump( array_filter( $array3, filter(...), ARRAY_FILTER_USE_BOTH | ARRAY_FILTER_PACK_RESULT ) );

$array4 = [
123,
true,
false,
(object)[],
[],
456,
'123',
];
var_dump( array_filter( $array4, is_int(...) ) );
var_dump( array_filter( $array4, is_int(...), ARRAY_FILTER_PACK_RESULT ) );

$array4 = [
'first int' => 123,
'true' => true,
'false' => false,
'object' => (object)[],
'array' => [],
'second int' => 456,
'int-looking string' => '123',
];
var_dump( array_filter( $array4, is_int(...) ) );
var_dump( array_filter( $array4, is_int(...), ARRAY_FILTER_PACK_RESULT ) );

?>
--EXPECT--
array(3) {
[2]=>
bool(true)
[4]=>
string(3) "foo"
[6]=>
int(123)
}
array(3) {
[0]=>
bool(true)
[1]=>
string(3) "foo"
[2]=>
int(123)
}
array(1) {
[1]=>
bool(false)
}
array(1) {
[0]=>
bool(false)
}
Filter: $key => $value\nFilter: $key => $value\nFilter: $key => $value\narray(3) {
["key1"]=>
bool(true)
["key2"]=>
bool(false)
["key3"]=>
int(123)
}
Filter: $key => $value\nFilter: $key => $value\nFilter: $key => $value\narray(3) {
[0]=>
bool(true)
[1]=>
bool(false)
[2]=>
int(123)
}
array(2) {
[0]=>
int(123)
[5]=>
int(456)
}
array(2) {
[0]=>
int(123)
[1]=>
int(456)
}
array(2) {
["first int"]=>
int(123)
["second int"]=>
int(456)
}
array(2) {
[0]=>
int(123)
[1]=>
int(456)
}
Loading