PHP: Removing all non-numeric characters from an array

Updated: January 10, 2024 By: Guest Contributor Post a comment

Introduction

When working with arrays in PHP, it is common to encounter situations where you need to sanitize array elements, removing non-numeric characters to perform calculations or standardize data formats. PHP offers a variety of functions to accomplish this. We’ll explore simple to advanced methods, including built-in functions, regex, and custom filtering strategies.

Basic method: Using preg_replace

One of the simplest ways to remove non-numeric characters from an array is by using the preg_replace function, with a regular expression that matches all non-digit characters:

$array = ['item1', '23', 'hello4', '5world'];
$cleanedArray = array_map(function($item) {
    return preg_replace('/[^0-9]/', '', $item);
}, $array);
print_r($cleanedArray);

The output will be:

Array
(
    [0] => 
    [1] => 23
    [2] => 4
    [3] => 5
)

This method is quick, but it operates on each element separately and may not be the most efficient for larger arrays.

Intermediate method: Using array_filter and is_numeric

If you’re looking to remove entire array elements that are non-numeric, rather than just the characters, array_filter combined with is_numeric may be more suitable:

$array = ['100', 'text', '300', '456abc'];
$numericArray = array_filter($array, 'is_numeric');
print_r($numericArray);

The output will be:

Array
(
    [0] => 100
    [2] => 300
)

This method is particularly useful when you want to preserve purely numeric data in your array.

Advanced method: Custom function for complex filtering

When neither of the above methods suit your needs, you may opt to write a custom function. Below is an example of a function that cleans an array, removing any non-numeric characters and converting numeric strings to integers.

function cleanArray($array) {
    return array_map(function($item) {
        $cleaned = preg_replace('/[^0-9]/', '', $item);
        // Convert to integer if numeric
        return is_numeric($cleaned) ? (int)$cleaned : $cleaned;
    }, $array);
}
$array = ['2cats', '3dogs', 'fish4'];
$cleanedArray = cleanArray($array);
print_r($cleanedArray);

The output will be:

Array
(
    [0] => 2
    [1] => 3
    [2] => 4
)

This custom function offers flexibility and can be expanded for more complex criteria and data transformations.

Using Regular Expressions for Granular Control

Regular expressions allow for fine-tuned string manipulation. PHP’s preg_replace_callback function can be used alongside regex to create sophisticated character removal logic that can be applied conditionally:

function selectivelyClean($array) {
    return array_map(function($item) {
        return preg_replace_callback('/(\D+)/', function($matches) {
            // Add logic here to decide which non-numeric characters to remove
            return ''; // This example simply removes all non-digits
        }, $item);
    }, $array);
}
$array = ['foo1', 'bar2', 'baz3'];
$cleanedArray = selectivelyClean($array);
print_r($cleanedArray);

The output will be:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

This method uses a callback function to provide context-aware processing of each array element.

Conclusion

PHP provides multiple approaches to sanitizing arrays, from quick and simple regex-based solutions to comprehensive custom filtering. Deciding on the right method depends on the specific requirements of your task and the nature of the data you’re working with. Apply these techniques to ensure that your arrays contain only the numeric data you need for reliable application performance.