Automattic\WooCommerce\Utilities

ArrayUtil::to_ranges_string()public staticWC 1.0

Converts an array of numbers to a human-readable range, such as "1,2,3,5" to "1-3, 5". It also supports floating point numbers, however with some perhaps unexpected / undefined behaviour if used within a range. Source: https://stackoverflow.com/a/34254663/4574

{} Это метод класса: ArrayUtil{}

Хуков нет.

Возвращает

Строку.

Использование

$result = ArrayUtil::to_ranges_string( $items, $item_separator, $range_separator, $sort ): string;
$items(массив) (обязательный)
An array (in any order, see $sort) of individual numbers.
$item_separator(строка)
The string that separates sequential range groups.
По умолчанию: ', '
$range_separator(строка)
The string that separates ranges. A plausible example otherwise would be ' to '.
По умолчанию: '-'
$sort(true|false|true)
Sort the array prior to iterating? You'll likely always want to sort, but if not, you can set this to false.
По умолчанию: true

Код ArrayUtil::to_ranges_string() WC 6.7.0

public static function to_ranges_string( array $items, string $item_separator = ', ', string $range_separator = '-', bool $sort = true ): string {
	if ( $sort ) {
		sort( $items );
	}

	$point = null;
	$range = false;
	$str   = '';

	foreach ( $items as $i ) {
		if ( null === $point ) {
			$str .= $i;
		} elseif ( ( $point + 1 ) === $i ) {
			$range = true;
		} else {
			if ( $range ) {
				$str  .= $range_separator . $point;
				$range = false;
			}
			$str .= $item_separator . $i;
		}
		$point = $i;
	}

	if ( $range ) {
		$str .= $range_separator . $point;
	}

	return $str;
}