-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add FilterNum() in ArrayHelper.php #43
base: 3.x-dev
Are you sure you want to change the base?
Add FilterNum() in ArrayHelper.php #43
Conversation
I believe this feature is not required as the task is fairly simple when using native PHP. Let me explain: Filter numbers onlyAll supported PHP versions array_filter($array, 'is_numeric') Filter positive numbers onlyPlease note that the same can be extended for negative numbers, even numbers, odd numbers and various other conditions easily. PHP 7.4 onwards array_filter($array, fn($val) => is_numeric($val) && $val >= 0) PHP below 7.4 array_filter($array, function($val) {
return is_numeric($val) && $val >= 0;
}); |
|
The trick is for the array to return TRIM() strings at the same time. And the digit 0 was also considered a Valid value in the array. |
@korenevskiy I agree to your use case. Still I truly believe that May be |
You have combined 2 tasks into 1. 1.Splitting a string into an array. 2. Filtering the array. In principle, combining 2 tasks is useful. But it is useful for internal calculations. And the use of the helper class has a wider use in different places. It may turn out that you just need to filter the array without dividing the string into an array. So I suggested a simple filtering method. Another question is the intuitiveness of the code. How would an average programmer write code. KeywordSearch::toIds($str); or ArrayHelper::filterIds(explode(',',$str)); //or ArrayHelper::filterNum() I would use only the second option. Although the first one is shorter, but it's not intuitive for me. |
Just in case the team decides to go with the approach in this PR, I would add few suggestions:
|
I changed the PR according to your recommendations . They are weighty. Except deleting $i. Since the static method removes filters array elements, this should save the array keys. Do you think the array keys are not needed? |
Right, but as the name suggests |
as if there is still a function is_num(). it accepts a string and the string can be regarded as a number. It is by analogy of this function that I originally called the filterNum() method. If it were C#, I would really make the parameter type more typed. But here we have PHP, where one function serves for all types. |
"The |
as if there is still a function is_num(). it accepts a string and the string can be regarded as a number. It is by analogy of this function that I originally called the filterNum() method. If it were C#, I would really make the parameter type more typed. But here we have PHP, where one function serves for all types. |
Pull Request for Issue #
A function for filtering only numbers from an array of values. Used to filter values to insert into an SQL query.
Testing Instructions
return
unlike the array_filter() function, this function array to return TRIM() strings at the same time. And the digit 0 was also considered a Valid value in the array.
This is necessary to filter indexes for insertion into the database.