rest_validate_array_contains_unique_items

函式
rest_validate_array_contains_unique_items ( $array )
引數
  • (array) $array The array to check.
    Required:
返回值
  • (bool) True if the array contains unique items, false otherwise.
定義位置
相關方法
rest_validate_array_value_from_schemarest_validate_string_value_from_schemarest_validate_request_argrest_validate_boolean_value_from_schemarest_validate_value_from_schema
引入
5.5.0
棄用
-

rest_validate_array_contains_unique_items: 這是一個WordPress的函式,驗證一個陣列是否只包含唯一的值: 該函式接受一個陣列引數,如果陣列中的任何值不是唯一的,則返回一個錯誤資訊。

檢查一個陣列是否是由唯一的項組成的。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function rest_validate_array_contains_unique_items( $array ) {
$seen = array();
foreach ( $array as $item ) {
$stabilized = rest_stabilize_value( $item );
$key = serialize( $stabilized );
if ( ! isset( $seen[ $key ] ) ) {
$seen[ $key ] = true;
continue;
}
return false;
}
return true;
}
function rest_validate_array_contains_unique_items( $array ) { $seen = array(); foreach ( $array as $item ) { $stabilized = rest_stabilize_value( $item ); $key = serialize( $stabilized ); if ( ! isset( $seen[ $key ] ) ) { $seen[ $key ] = true; continue; } return false; } return true; }
function rest_validate_array_contains_unique_items( $array ) {
	$seen = array();

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

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

			continue;
		}

		return false;
	}

	return true;
}

常見問題

FAQs
檢視更多 >