PHP Laravel Convert array value to upper case

PHP Laravel Convert array value to upper case

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Laravel Convert Array Value to Upper Case

When processing large datasets in a Laravel application, you often encounter scenarios where specific fields within an array need normalization before further processing or storage. Consider a dataset containing contact information for multiple records, where the province field requires uppercase conversion while all other fields must remain unchanged:

array:1434 [▼
  0 => array:53 [▼
    "contact" => "ANA (dependienta)"
    "mail" => "example@mail.com"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "Zaragoza"
  ]
  // ... more items
]

The goal is to transform only the province value to uppercase, resulting in:

array:1434 [▼
  0 => array:53 [▼
    "contact" => "ANA (dependienta)"
    "mail" => "example@mail.com"
    // ... other fields unchanged
    "province" => "ZARAGOZA"
  ]
]

Using Laravel Collections for Targeted Transformation

The most idiomatic approach in Laravel is leveraging the collect() helper to convert your raw array into a collection. This allows you to utilize chainable methods that are both readable and efficient.

To achieve the specific requirement of updating only the province key, you can use the map() method combined with conditional logic inside a closure:

$collection = collect($originalArray);

$processedCollection = $collection->map(function ($item) {
    if (isset($item['province'])) {
        $item['province'] = strtoupper($item['province']);
    }
    
    return $item;
});

This approach iterates over every item in the collection. The if statement ensures that the transformation only occurs on items where the province key actually exists, preventing potential notices or unexpected behavior if some records are incomplete. The result remains a collection object, which you can convert back to an array using ->toArray() if you need to send it to another layer of your application.

Alternatively, you can use the transform() method if you prefer to mutate the original structure without creating a new variable immediately:

$collection = collect($originalArray)->transform(function ($item) {
    $item['province'] = strtoupper($item['province']);
    return $item;
});

Note that this version will throw an Undefined array key exception if the province key is missing. If your dataset might contain incomplete records, stick with the map() approach including the existence check.

Performance Considerations for Large Datasets

Your source material indicates a dataset size of 1434 items, which is manageable but worth considering when scaling. The map() method iterates through each element once, resulting in O(n) complexity relative to the number of top-level arrays. Inside the closure, the operation is constant time O(1) because it only touches one key per item.

If you were dealing with millions of records, you might consider using a single native array_walk() function instead, as it avoids the overhead of creating a collection object and its internal iterator:

array_walk($originalArray, function (&$item) {
    if (isset($item['province'])) {
        $item['province'] = strtoupper($item['province']);
    }
});

However, for typical web application payloads under a few thousand items, the Collection method is preferred due to its readability and seamless integration with other Laravel helpers.

Avoiding Common Pitfalls

A frequent mistake developers make when using strtoupper() on mixed-content fields is ignoring special characters or encoding issues. In your example, the contact field contains parentheses and asterisks (e.g., "ANA (dependienta)"). The strtoupper() function handles ASCII characters correctly, but if your data includes non-ASCII characters (like accented letters in Spanish provinces), you might need to ensure the correct locale is set or use a library like Intl for robust handling.

Another pitfall is modifying the original array directly inside the loop without returning the modified item when using map(). Always remember that map() returns a new collection; it does not mutate the original array in place unless you explicitly assign it back or convert it first.

Final Recommendation

For this specific use case, converting the array to a Laravel Collection and using map() with a conditional check for the province key offers the best balance of safety, readability, and maintainability. This pattern aligns well with Laravel’s philosophy of making data manipulation explicit and composable.

Our network