Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/10/2016 05:14:34 PM (9 years ago)
Author:
dcavins
Message:

bp_sort_by_key(): Add $preserve_keys parameter.

Add a parameter to bp_sort_by_key() so that it calls either usort()
or uasort(), depending on the whether the array keys are important or
not.

Fixes #7238.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r11081 r11082  
    109109 *
    110110 * @since 2.2.0
    111  *
    112  * @param array      $items The items to be sorted. Its constituent items can be either associative arrays or objects.
    113  * @param string|int $key   The array index or property name to sort by.
    114  * @param string     $type  Sort type. 'alpha' for alphabetical, 'num' for numeric. Default: 'alpha'.
     111 * @since 2.7.0 Added $preserve_keys parameter.
     112 *
     113 * @param array      $items         The items to be sorted. Its constituent items
     114 *                                  can be either associative arrays or objects.
     115 * @param string|int $key           The array index or property name to sort by.
     116 * @param string     $type          Sort type. 'alpha' for alphabetical, 'num'
     117 *                                  for numeric. Default: 'alpha'.
     118 * @param bool       $preserve_keys Whether to keep the keys or not.
     119 *
    115120 * @return array $items The sorted array.
    116121 */
    117 function bp_sort_by_key( $items, $key, $type = 'alpha' ) {
    118     usort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) );
     122function bp_sort_by_key( $items, $key, $type = 'alpha', $preserve_keys = false ) {
     123    if ( true === $preserve_keys ) {
     124        uasort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) );
     125    } else {
     126        usort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ) );
     127    }
    119128
    120129    return $items;
Note: See TracChangeset for help on using the changeset viewer.