Skip to content

Arr::dot() doesn't flatten empty arrays #57305

Description

@ropi-bc

Laravel Version

12.26

PHP Version

8.3.9

Database Driver & Version

No response

Description

I have a Filament feature that finds differences between the before and after of a record after editing. I'm starting out with:

$data = array (
  'name' => 'láadwáldalwéáasmnlkadswwadwa',
  'description' => NULL,
  'dimension_values' => 
  array (
    1 => 1,
    2 => 15,
    3 => NULL,
    4 => 134,
    5 => 149,
  ),
)

$oldState = array (
  'id' => 78233,
  'name' => 'láadwáldalwéáasmnlkadsw',
  'created_at' => '2025-10-02T14:02:09.000000Z',
  'updated_at' => '2025-10-08T09:23:46.000000Z',
  'type' => 4,
  'description' => NULL,
  'tags' => 
  array (
  ),
  'dimension_values' => 
  array (
    1 => '1',
    2 => '15',
    3 => NULL,
    4 => '134',
    5 => '149',
  ),
)

I can't call array_diff() on these because both arrays are multi-dimensional, so I tried Arr::dot(), which works well for the most part, but what I see instead is that the empty array $oldState['tags'] remained untouched. This is problematic because the result still counts as a multi-dimensional array, which means that I still can't use it as input to array_diff().

Based on the description (Flatten a multi-dimensional associative array with dots.), I'd expect the output to be completely flattened.

Is this really the expected output of this function? I'd expect the output to be either:

  array (
    'id' => 78233,
    'name' => 'láadwáldalwéáasmnlkadswwa',
    'created_at' => '2025-10-02T14:02:09.000000Z',
    'updated_at' => '2025-10-08T12:14:38.000000Z',
    'type' => 4,
    'description' => NULL,
    'dimension_values.1' => '1',
    'dimension_values.2' => '15',
    'dimension_values.3' => NULL,
    'dimension_values.4' => '134',
    'dimension_values.5' => '149',
  )

Or:

  array (
    'id' => 78233,
    'name' => 'láadwáldalwéáasmnlkadswwa',
    'created_at' => '2025-10-02T14:02:09.000000Z',
    'updated_at' => '2025-10-08T12:14:38.000000Z',
    'type' => 4,
    'description' => NULL,
    'tags' => NULL,
    'dimension_values.1' => '1',
    'dimension_values.2' => '15',
    'dimension_values.3' => NULL,
    'dimension_values.4' => '134',
    'dimension_values.5' => '149',
  )

Steps To Reproduce

<?php

use Illuminate\Support\Collection;

it('after calling dot() on a collection, I should see no empty arrays', function () {
    $data = [
        'user' => [
            'name' => 'John',
            'address' => [
                'street' => 'Main St',
                'empty_array' => [],
                'city' => 'New York',
            ],
            'empty_array' => [],
        ],
    ];

    $dotted = collect($data)->dot();

    expect($dotted['user.address.empty_array'])->not->toBeArray();
    expect($dotted['user.empty_array'])->not->toBeArray();
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Fields

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions