-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[12.x] Add Arr::isFlat() #56527
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
[12.x] Add Arr::isFlat() #56527
Conversation
public static function isFlat($array): bool | ||
{ | ||
foreach ($array as $entry) { | ||
if (is_array($entry)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
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.
This can be simplified to the following once #56526 is approved:
public static function isFlat($array): bool | |
{ | |
foreach ($array as $entry) { | |
if (is_array($entry)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static function isFlat($array): bool | |
{ | |
return self::every($array, fn ($value, $key) => ! is_array($value)); | |
} |
public static function isFlat($array): bool | ||
{ | ||
foreach ($array as $entry) { | ||
if (is_array($entry)) { |
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.
Do we need to support values that are objects and instances of either Arrayable or ArrayAccess?
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 would consider arrays containing objects flat, however, I'd say, you might have a point with Arrayable
and ArrayAccess
, though 🤔
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! |
This function determines if no other arrays are nested in the given array.
Examples
Notes
Currently, an array is only considered non-flat when it contains other arrays. Containing other non-scalars doesn't make them non-flat with the current implementation.