Automattic\WooCommerce\StoreApi\Utilities

ArrayUtils::natural_language_join()public staticWC 1.0

Join a string with a natural language conjunction at the end.

Method of the class: ArrayUtils{}

No Hooks.

Return

String. a string containing a list of items and a natural language conjuction.

Usage

$result = ArrayUtils::natural_language_join( $array, $enclose_items_with_quotes );
$array(array) (required)
The array to join together with the natural language conjunction.
$enclose_items_with_quotes(true|false)
Whether each item in the array should be enclosed within quotation marks.
Default: false

ArrayUtils::natural_language_join() code WC 8.7.0

public static function natural_language_join( $array, $enclose_items_with_quotes = false ) {
	if ( true === $enclose_items_with_quotes ) {
		$array = array_map(
			function( $item ) {
				return '"' . $item . '"';
			},
			$array
		);
	}
	$last = array_pop( $array );
	if ( $array ) {
		return sprintf(
			/* translators: 1: The first n-1 items of a list 2: the last item in the list. */
			__( '%1$s and %2$s', 'woocommerce' ),
			implode( ', ', $array ),
			$last
		);
	}
	return $last;
}