Skip to content

v1.1.0 - Advanced Array Handling

Latest

Choose a tag to compare

@provydon provydon released this 15 Oct 23:14
· 2 commits to main since this release

🎉 New Features

1. Auto-Flatten Single-Item Arrays

The new flattenSingleArrays() method automatically extracts single items from arrays without adding the "#1" suffix - perfect for form/questionnaire data structures.

JsonToKeyvalue::make($data, 'User')
    ->flattenSingleArrays(true)
    ->toFields();

2. Skip Array Indices

Use skipArrayIndices() to completely skip adding array indices to field labels.

JsonToKeyvalue::make($data, 'Users')
    ->skipArrayIndices(true)
    ->toFields();

3. Custom Array Index Formatting

Customize array index format with arrayIndexFormat() - use any sprintf format string.

// Use parentheses instead of #
JsonToKeyvalue::make($data, 'User')
    ->arrayIndexFormat(' (%d)')
    ->toFields();

// Use brackets
JsonToKeyvalue::make($data, 'User')
    ->arrayIndexFormat(' [%d]')
    ->toFields();

4. Conditional Flattening

Only flatten arrays below a certain size with maxArraySize().

JsonToKeyvalue::make($data, 'Users')
    ->maxArraySize(10)
    ->toFields();

5. Better Nested Iteration

The new static method fromNestedArray() simplifies iterating through keyed structures, automatically using keys as labels.

Before:

collect($this->micro_credit_data)->flatMap(function ($value, $key) {
    $label = ucwords(str_replace('_', ' ', $key));
    $data = is_array($value) && isset($value[0]) ? $value[0] : $value;
    return JsonToKeyvalue::make($data, $label)->flattenNested(true)->toFields();
})->toArray()

After:

JsonToKeyvalue::fromNestedArray($this->micro_credit_data)

With custom label formatter:

JsonToKeyvalue::fromNestedArray($data, fn($key) => strtoupper($key))

🔧 Configuration

New configuration options available in config/json-to-keyvalue.php:

'flatten_single_arrays' => false,
'skip_array_indices' => false,
'array_index_format' => ' #%d',
'max_array_size' => null,

✅ Tests

  • Added 11 comprehensive tests for new features
  • All 27 tests passing with 35 assertions
  • Full backward compatibility maintained

📦 Installation

composer require provydon/json-to-keyvalue:^1.1

Or update existing installation:

composer update provydon/json-to-keyvalue

🙏 Credits

Thanks to the community for suggesting these improvements!