Skip to content

[12.x] Add Arr::intval helper to convert and optionally filter integer values #56045

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

Closed
wants to merge 1 commit into from

Conversation

dutchman1990
Copy link

feat(arr): Arr::intval helper in Array helpers

  • Adds a static Arr::intval(array $value, bool $filterZeros = true): array method
  • Converts array values to integers using array_map
  • Optionally filters out zero values using Arr::where
  • Useful for sanitizing input arrays (e.g., form inputs, query params)

🧪 Example: Using Arr::intval to Convert and Optionally Filter Integer Values

use App\Support\Arr;

// ✅ Default: filter out zero values (0 and '0')
$result = Arr::intval(['10', '0', '25', 0, 'abc']);
// Result: [10, 25]

// ❌ Disable filtering: keep all values including zeros
$result = Arr::intval(['10', '0', '25', 0, 'abc'], false);
// Result: [10, 0, 25, 0, 0]

…teger values

- Adds a static Arr::intval(array $value, bool $filterZeros = true): array method
- Converts array values to integers using array_map
- Optionally filters out zero values using Arr::where
- Useful for sanitizing input arrays (e.g., form inputs, query params)
@shaedrich
Copy link
Contributor

Why filtering out zeros? Aren't they valid integers?

@dutchman1990
Copy link
Author

Why filtering out zeros? Aren't they valid integers?

Yes, they are. In my POV that flag could be useful if you don't need to consider 0. Or we can reverse the logic.

@shaedrich
Copy link
Contributor

Yeah, well, but that's still a rather arbitrary filter. Next, people want another built-in filter. That doesn't sound reasonable enough.

@dutchman1990
Copy link
Author

@shaedrich is it arbitrary? Yes, maybe, but one built in method is always welcome. Plus I had to write array_map of several occasions this could be useful with just single line code with laravel wrapper.

@shaedrich
Copy link
Contributor

I wasn't arguing against the whole PR, but rather the zeros flag

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@dennisprudlo
Copy link
Contributor

@dutchman1990 the Array class is macroable so you can easily add this function yourself to use it as an alias for your array_map calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants