rest_sanitize_array()WP 5.5.0

Converts an array-like value to an array.

No Hooks.

Return

Array. Returns the array extracted from the value.

Usage

rest_sanitize_array( $maybe_array );
$maybe_array(mixed) (required)
The value being evaluated.

Examples

0

#1 What the function returns

rest_sanitize_array( 'Vova,Misha,Timur,Dima' );

/* return
Array (
	[0] => Vova
	[1] => Misha
	[2] => Timur
	[3] => Dima
)
*/
rest_sanitize_array( [ 
	'name' => 'Olga', 
	'age' => 5 
] );

/* return
Array (
	[0] => Olga
	[1] => 5
)
*/

Changelog

Since 5.5.0 Introduced.

rest_sanitize_array() code WP 6.5.2

function rest_sanitize_array( $maybe_array ) {
	if ( is_scalar( $maybe_array ) ) {
		return wp_parse_list( $maybe_array );
	}

	if ( ! is_array( $maybe_array ) ) {
		return array();
	}

	// Normalize to numeric array so nothing unexpected is in the keys.
	return array_values( $maybe_array );
}