Changeset 11362 for trunk/src/bp-core/bp-core-functions.php
- Timestamp:
- 12/29/2016 09:13:41 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-functions.php
r11285 r11362 121 121 */ 122 122 function bp_sort_by_key( $items, $key, $type = 'alpha', $preserve_keys = false ) { 123 $callback = function( $a, $b ) use ( $key, $type ) { 124 $values = array( 0 => false, 1 => false ); 125 $func_args = func_get_args(); 126 foreach ( $func_args as $indexi => $index ) { 127 if ( isset( $index->{$key} ) ) { 128 $values[ $indexi ] = $index->{$key}; 129 } elseif ( isset( $index[ $key ] ) ) { 130 $values[ $indexi ] = $index[ $key ]; 131 } 132 } 133 134 if ( isset( $values[0], $values[1] ) ) { 135 if ( 'num' === $type ) { 136 $cmp = $values[0] - $values[1]; 137 } else { 138 $cmp = strcmp( $values[0], $values[1] ); 139 } 140 141 if ( 0 > $cmp ) { 142 $retval = -1; 143 } elseif ( 0 < $cmp ) { 144 $retval = 1; 145 } else { 146 $retval = 0; 147 } 148 return $retval; 149 } else { 150 return 0; 151 } 152 }; 153 123 154 if ( true === $preserve_keys ) { 124 uasort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ));155 uasort( $items, $callback ); 125 156 } else { 126 usort( $items, array( new BP_Core_Sort_By_Key_Callback( $key, $type ), 'sort_callback' ));157 usort( $items, $callback ); 127 158 } 128 159
Note: See TracChangeset
for help on using the changeset viewer.