Skip to content

[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

Closed
wants to merge 4 commits into from
Closed

Conversation

shaedrich
Copy link
Contributor

@shaedrich shaedrich commented Aug 3, 2025

This function determines if no other arrays are nested in the given array.

Examples

Arr::isFlat([1, 'foo', 5 => 2.3, 'bar' => 'baz', new stdClass, true]); // true
Arr::isFlat([1, 2, [3]]); // false
Arr::isFlat([[1]]); // also false

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.

Comment on lines +600 to +609
public static function isFlat($array): bool
{
foreach ($array as $entry) {
if (is_array($entry)) {
return false;
}
}

return true;
}
Copy link
Contributor Author

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:

Suggested change
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)) {

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?

Copy link
Contributor Author

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 🤔

@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!

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.

3 participants