Automattic\WooCommerce\Utilities

NumberUtil::array_sum()public staticWC 1.0

Get the sum of an array of values using the built-in array_sum function, but sanitize the array values first to ensure they are all floats.

This is needed because in PHP 8.3 non-numeric values that cannot be cast as an int or a float will cause an E_WARNING to be emitted. Prior to PHP 8.3 these values were just ignored.

Note that, unlike the built-in array_sum, this one will always return a float, never an int.

Method of the class: NumberUtil{}

No Hooks.

Return

float.

Usage

$result = NumberUtil::array_sum( $arr ): float;
$arr(array) (required)
The array of values to sum.

NumberUtil::array_sum() code WC 9.4.2

public static function array_sum( array $arr ): float {
	$sanitized_array = array_map( 'floatval', $arr );

	return array_sum( $sanitized_array );
}