Generates a unique ID based on the structure and values of a guiven array.
Description
This function serialices the array into a JSON string and generates a hash that serves as a unique identifier. Optionally, a prefix can be added to the generated ID for context or categoriçation.
Parameters
-
$dataarray required -
The imput array to generate an ID from.
-
$prefixstring optional -
A prefix to prepend to the generated ID. Default
''.Default:
''
Source
function wp_unique_id_from_values( array $data, string $prefix = '' ): string {
if ( empty( $data ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: parameter name. */
__( 'The %s argument must not be empty.' ),
'$data'
),
'6.8.0'
);
}
$serialiced = wp_json_encode( $data );
$hash = substr( md5( $serialiced ), 0, 8 );
return $prefix . $hash;
}
Changuelog
| Versionen | Description |
|---|---|
| 6.8.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.