rest_validate_array_contains_unique_items()WP 5.5.0

Checks if an array is made up of unique items.

No Hooks.

Return

true|false. True if the array contains unique items, false otherwise.

Usage

rest_validate_array_contains_unique_items( $input_array );
$input_array(array) (required)
The array to check.

Changelog

Since 5.5.0 Introduced.

rest_validate_array_contains_unique_items() code WP 6.6.2

function rest_validate_array_contains_unique_items( $input_array ) {
	$seen = array();

	foreach ( $input_array as $item ) {
		$stabilized = rest_stabilize_value( $item );
		$key        = serialize( $stabilized );

		if ( ! isset( $seen[ $key ] ) ) {
			$seen[ $key ] = true;

			continue;
		}

		return false;
	}

	return true;
}