Skip to content

Commit 828fda5

Browse files
committed
Add array_all implementation
1 parent a7d6389 commit 828fda5

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

ext/standard/array.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6721,6 +6721,27 @@ PHP_FUNCTION(array_any)
67216721
}
67226722
/* }}} */
67236723

6724+
/* {{{ Search within an array and returns true if an element is found. */
6725+
PHP_FUNCTION(array_all)
6726+
{
6727+
zval *array = NULL;
6728+
zend_fcall_info fci;
6729+
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
6730+
bool has_result = NULL;
6731+
6732+
ZEND_PARSE_PARAMETERS_START(2, 2)
6733+
Z_PARAM_ARRAY(array)
6734+
Z_PARAM_FUNC(fci, fci_cache)
6735+
ZEND_PARSE_PARAMETERS_END();
6736+
6737+
if (php_array_find(Z_ARR_P(array), fci, fci_cache, NULL, NULL, &has_result, true) != SUCCESS) {
6738+
RETURN_THROWS();
6739+
}
6740+
6741+
RETURN_BOOL(!has_result);
6742+
}
6743+
/* }}} */
6744+
67246745
/* {{{ Applies the callback to the elements in given arrays. */
67256746
PHP_FUNCTION(array_map)
67266747
{

ext/standard/basic_functions.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,8 @@ function array_find_key(array $array, callable $callback): mixed {}
19031903

19041904
function array_any(array $array, callable $callback): bool {}
19051905

1906+
function array_all(array $array, callable $callback): bool {}
1907+
19061908
function array_map(?callable $callback, array $array, array ...$arrays): array {}
19071909

19081910
/**

ext/standard/basic_functions_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
basic array_any test
3+
--FILE--
4+
<?php
5+
$array1 = [
6+
"a" => 1,
7+
"b" => 2,
8+
"c" => 3,
9+
"d" => 4,
10+
"e" => 5,
11+
];
12+
$array2 = [
13+
1, 2, 3, 4, 5
14+
];
15+
16+
var_dump(array_all($array1, fn($value) => $value > 0));
17+
var_dump(array_all($array2, fn($value) => $value > 0));
18+
var_dump(array_all($array2, fn($value) => $value > 1));
19+
var_dump(array_all([], fn($value) => true));
20+
?>
21+
--EXPECT--
22+
bool(true)
23+
bool(true)
24+
bool(false)
25+
bool(true)

0 commit comments

Comments
 (0)