Convert a comma-delimited string into array of integers?

Convert a comma-delimited string into array of integers?

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

Stefan Bogdanescu

Founder & Senior Architect

Casting Strings to Integers After explode()

When you split a comma-delimited string like "1,2,3" using PHP's explode() function, the resulting array contains strings, even if the original characters look numeric:

$string = "1,2,3";
$ids = explode(',', $string);

// $ids is actually:
// [0] => "1" (string)
// [1] => "2" (string)
// [2] => "3" (string)

If your downstream logic expects integers—for example, when passing IDs to a database query, performing arithmetic, or caching values—you need to convert those strings. While looping with foreach and casting each element is functionally correct, there are more concise and expressive alternatives that align better with modern PHP idioms.

Using array_map for Concise Conversion

The most direct improvement over a manual loop is array_map(). It applies a callback to every element of the array and returns a new array with the results:

$string = "1,2,3";
$ids = array_map('intval', explode(',', $string));

// Now $ids contains integers:
// [0] => 1 (int)
// [1] => 2 (int)
// [2] => 3 (int)

This approach keeps the transformation in a single, readable line. It avoids the boilerplate of foreach and makes the intent explicit: "map each string to an integer." The built-in intval() function handles the casting efficiently and is optimized in the C level of PHP.

Handling Edge Cases

The simplicity of array_map('intval', ...) can be misleading if you don't consider what happens with malformed input. intval() converts non-numeric strings to 0, which might silently corrupt your data:

$ids = array_map('intval', explode(',', "1,2,bad,4"));
// Result: [1, 2, 0, 4]

If your source string is unreliable, a more robust approach filters out invalid values before casting:

$string = "1,2,bad,4";
$ids = array_map(function ($val) {
    if (is_numeric($val)) {
        return (int)$val;
    }
    return null; // or throw an exception, depending on your needs
}, explode(',', $string));

// Result: [1, 2, null, 4]

Alternatively, you can use a filter to keep only valid integers and discard the rest:

$ids = array_filter(
    explode(',', $string),
    fn($v) => is_numeric($v)
);

$ids = array_map('intval', $ids);

Choose the strategy that matches your error-handling requirements. If a malformed value should cause the operation to fail, throw an exception inside the callback instead of returning null.

Performance Considerations

For large datasets, the performance difference between foreach and array_map() is negligible because both iterate at C speed internally. The real bottleneck is usually I/O or database operations, not the casting itself. However, readability often outweighs micro-optimizations. If you're processing thousands of IDs in a tight loop within an application that runs frequently, consider whether the comma-delimited format is the right choice to begin with. Storing IDs as a space-separated string or a JSON array can simplify parsing and avoid repeated conversions.

When to Use Laravel's Helpers

If you're working within a Laravel application, the same logic applies, but you might prefer using Laravel's collection helpers for chaining operations:

use Illuminate\Support\Collection;

$ids = (new Collection(explode(',', $string)))
    ->map(fn($v) => is_numeric($v) ? (int)$v : null);

This keeps your transformation logic within the collection pipeline, making it easier to filter, sort, or pass along to other services. The underlying mechanism remains the same: intval() does the heavy lifting, and array_map or Laravel's map() provides a clean API for applying it.

Final Recommendation

Stick with array_map('intval', ...) when the input is trusted and you want minimal code. Add validation logic if your data might contain non-numeric values. Avoid manual loops unless you need complex branching inside each iteration. Whether you're working in vanilla PHP or Laravel, the goal is the same: keep transformations explicit, safe, and easy to reason about.

Our network