rest_validate_array_contains_unique_items() WordPress Function

The rest_validate_array_contains_unique_items() function is used to validate that an array contains only unique items. This is useful when validating user input, to ensure that duplicate values are not entered.

rest_validate_array_contains_unique_items( array $array ) #

Checks if an array is made up of unique items.


Parameters

$array

(array)(Required)The array to check.


Top ↑

Return

(bool) True if the array contains unique items, false otherwise.


Top ↑

Source

File: wp-includes/rest-api.php

1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
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;
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More