ap_sort_array_by_order( array $arr )

Description #

Sort array by order value. Group array which have same order number and then sort them.

Parameters #

  • $arr
    array (Required) Array to order.

Changelog #

VersionDescription
4.1.0Use WP_List_Util class for sorting.
2.0.0Introduced.

Source #

File: includes/functions.php

function ap_sort_array_by_order( $arr ) {
	$new_array = array();

	if ( ! empty( $arr ) && is_array( $arr ) ) {
		$i = 1;
		foreach ( $arr as $k => $a ) {
			if ( is_array( $a ) ) {
				$arr[ $k ]['order'] = isset( $a['order'] ) ? $a['order'] : $i;
			}

			$i += 2;
		}

		$util = new WP_List_Util( $arr );
		return $util->sort( 'order', 'ASC', true );
	}
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment