-
Notifications
You must be signed in to change notification settings - Fork 7.9k
RFC: array_find #14108
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
Merged
Merged
RFC: array_find #14108
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cec1c62
Add `array_find` function
joshuaruesweg aefe7f3
Add `array_key_find` function
joshuaruesweg 664475c
Add `array_any` implementation
joshuaruesweg 8058c2f
Add `array_all` implementation
joshuaruesweg a8dfbbb
Remove helper variable `key` in php_array_find
joshuaruesweg 6e28b0f
Remove needless `fci.params = args` call in loop
joshuaruesweg c0768d6
Remove false initalisation of `has_results`
joshuaruesweg 19a80af
Remove `has_results` helper variable
joshuaruesweg e321daf
Add more tests for `array_find`
joshuaruesweg 77493d3
Add more tests for `array_all`
joshuaruesweg 72aeb73
Add more tests for `array_any`
joshuaruesweg 90803ef
Add more tests for `array_find_key`
joshuaruesweg 38c440f
Update `php_array_find` implementation
joshuaruesweg e3db846
Specify test descriptions
joshuaruesweg 4113c1f
[skip ci] NEWS/UPGRADING
TimWolla 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--TEST-- | ||
Test array_all() function : basic functionality | ||
--FILE-- | ||
<?php | ||
$array1 = [ | ||
"a" => 1, | ||
"b" => 2, | ||
"c" => 3, | ||
"d" => 4, | ||
"e" => 5, | ||
]; | ||
|
||
$array2 = [ | ||
1, 2, 3, 4, 5 | ||
]; | ||
|
||
function even($input) { | ||
return $input % 2 === 0; | ||
} | ||
|
||
class SmallerTenClass { | ||
public static function smallerTen($input) { | ||
return $input < 10; | ||
} | ||
} | ||
|
||
var_dump(array_all($array1, fn($value) => $value > 0)); | ||
var_dump(array_all($array2, fn($value) => $value > 0)); | ||
var_dump(array_all($array2, fn($value) => $value > 1)); | ||
var_dump(array_all([], fn($value) => true)); | ||
|
||
echo '*** Test Exception after false result ***' . PHP_EOL; | ||
try { | ||
var_dump(array_all($array2, function ($value) { | ||
if ($value > 1) { | ||
throw new Exception("Test-Exception"); | ||
} | ||
|
||
return false; | ||
})); | ||
} catch (Exception) { | ||
var_dump("Unexpected Exception"); | ||
} | ||
|
||
echo '*** Test aborting with exception ***' . PHP_EOL; | ||
try { | ||
var_dump(array_all($array2, function ($value) { | ||
if ($value === 2) { | ||
throw new Exception("Test-Exception"); | ||
} | ||
|
||
var_dump($value); | ||
|
||
return true; | ||
})); | ||
} catch (Exception) { | ||
var_dump("Catched Exception"); | ||
} | ||
|
||
var_dump(array_all($array1, 'even')); | ||
|
||
var_dump(array_all($array1, function($value) { | ||
// return nothing | ||
})); | ||
|
||
var_dump(array_all($array1, [ | ||
'SmallerTenClass', | ||
'smallerTen' | ||
])); | ||
|
||
var_dump(array_all($array1, "SmallerTenClass::smallerTen")); | ||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(true) | ||
bool(false) | ||
bool(true) | ||
*** Test Exception after false result *** | ||
bool(false) | ||
*** Test aborting with exception *** | ||
int(1) | ||
string(17) "Catched Exception" | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(true) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyone have an objection to making this
ZEND_API
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no disagreement. I have chosen a lower visibility for the time being, as it is always easier to increase visibility afterwards instead of reducing it. But if you see a more global use case for the method, we can make it
ZEND_API
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now merged this as-is. The
PHPAPI
can be added in a follow-up commit if necessary/desired.