wp_recursive_ksort()
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.
No Hooks.
Return
null
. Nothing.
Usage
wp_recursive_ksort( $array );
- $array(array) (required) (passed by reference — &)
- The array to sort, passed by reference.
Examples
#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() wp recursive ksort code WP 6.1.1
function wp_recursive_ksort( &$array ) { foreach ( $array as &$value ) { if ( is_array( $value ) ) { wp_recursive_ksort( $value ); } } ksort( $array ); }