Skip to content

Conversation

imhayatunnabi
Copy link

@imhayatunnabi imhayatunnabi commented Oct 13, 2025

Summary

  • Fixes Arr::dot() to properly flatten empty arrays by converting them to null
  • Updates existing tests to reflect the new behavior
  • Resolves issue where flattened arrays still contained nested empty arrays

Problem

The Arr::dot() method is documented to "flatten a multi-dimensional associative array with dots," but it was not handling empty arrays correctly. When an empty array was encountered, it was left as-is in the output, resulting in a partially flattened array that still contained nested structures.

Example of the problem:

$data = [
    'name' => 'John',
    'tags' => [],
    'address' => [
        'street' => 'Main St',
        'metadata' => [],
    ],
];

$result = Arr::dot($data);

// Before fix:
// [
//     'name' => 'John',
//     'tags' => [],                    // ❌ Still an array!
//     'address.street' => 'Main St',
//     'address.metadata' => [],        // ❌ Still an array!
// ]

// After fix:
// [
//     'name' => 'John',
//     'tags' => null,                  // ✅ Properly flattened
//     'address.street' => 'Main St',
//     'address.metadata' => null,      // ✅ Properly flattened
// ]

This caused issues when using the result with functions like array_diff() that require single-dimensional arrays.

Solution

Modified the dot() method in Arr.php to check if a value is an empty array and convert it to null instead of storing it directly. This ensures complete flattening.

Changes made:

src/Illuminate/Collections/Arr.php:179

// Before
$results[$newKey] = $value;

// After
$results[$newKey] = is_array($value) ? null : $value;

The Arr::dot() method was not properly flattening empty arrays, leaving
them as nested array values in the output. This prevented the result from
being fully flattened and caused issues when using functions like array_diff()
that require single-dimensional arrays.
The Arr::dot() method was not properly flattening empty arrays, leaving
them as nested array values in the output. This prevented the result from
being fully flattened and caused issues when using functions like array_diff()
that require single-dimensional arrays.
@ropi-bc
Copy link

ropi-bc commented Oct 13, 2025

your solution is nicer than mine, but at the same time Taylor closed my PR immediately, so probably this one will be rejected too. not a patch-version level fix.

->expectsOutput(' null .................................................. null ')
->expectsOutput(' array ⇁ 0 .. Illuminate\Foundation\Console\ConfigShowCommand ')
->expectsOutput(' empty_array ............................................. [] ')
->expectsOutputToContain('empty_array')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->expectsOutput(' empty_array ........................................... null ') would look nicer

Copy link
Author

@imhayatunnabi imhayatunnabi Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this was for the best test resiliency also had issue with php 8.3 testing. You can have look on the first commit

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

@imhayatunnabi imhayatunnabi deleted the fix/12.x-Arr.dot-issue-57305 branch October 13, 2025 16:41
@imhayatunnabi imhayatunnabi restored the fix/12.x-Arr.dot-issue-57305 branch October 13, 2025 16:41
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