wp_recursive_ksort()WP 6.0.0

Sorts the keys of an array alphabetically. The array is passed by reference so it doesn't get returned which mimics the behaviour of ksort.

The array is passed by reference so it doesn't get returned which mimics the behavior of ksort().

No Hooks.

Return

null. Nothing (null).

Usage

wp_recursive_ksort( $input_array );
$input_array(array) (required) (passed by reference — &)
The array to sort, passed by reference.

Examples

0

#1 Demo [auto-translate]

Sort the array and all its sub-arrays by keys in alphabetical order:

$array = [
	'two' => 2,
	'one' => [
		'two' => 2,
		'one' => [
			'two' => 2,
			'one' => 1,
		],
	],
];

wp_recursive_ksort( $array );

print_r( $array );

/*
Array
(
	[one] => Array
		(
			[one] => Array
				(
					[one] => 1
					[two] => 2
				)

			[two] => 2
		)

	[two] => 2
)
*/

Changelog

Since 6.0.0 Introduced.

wp_recursive_ksort() code WP 6.5.2

function wp_recursive_ksort( &$input_array ) {
	foreach ( $input_array as &$value ) {
		if ( is_array( $value ) ) {
			wp_recursive_ksort( $value );
		}
	}

	ksort( $input_array );
}