Changeset 9191 for trunk/src/bp-core/bp-core-functions.php
- Timestamp:
- 11/26/2014 06:21:42 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-functions.php
r9180 r9191 94 94 95 95 /** 96 * Sort an array of objects or arrays by alphabetically sorting by a specific key/property. 97 * 98 * For instance, if you have an array of WordPress post objects, you can sort 99 * them by post_name as follows: 100 * $sorted_posts = bp_alpha_sort_by_key( $posts, 'post_name' ); 96 * Sort an array of objects or arrays by a specific key/property. 101 97 * 102 98 * The main purpose for this function is so that you can avoid having to create 103 99 * your own awkward callback function for usort(). 104 100 * 105 * @since BuddyPress ( 1.9.0)106 * 107 * @param array $items The array to be sorted. Its constituent items can be108 * either associative arrays or objects.109 * @param string|int $key The array index or property name to sort by.110 * @return array $items The sorted array.111 */ 112 function bp_ alpha_sort_by_key( $items, $key) {101 * @since BuddyPress (2.2.0) 102 * 103 * @param array $items The items to be sorted. Its constituent items can be either associative arrays or objects. 104 * @param string|int $key The array index or property name to sort by. 105 * @param string $type Sort type. 'alpha' for alphabetical, 'num' for numeric. Default: 'alpha'. 106 * @return array $items The sorted array. 107 */ 108 function bp_sort_by_key( $items, $key, $type = 'alpha' ) { 113 109 usort( $items, create_function( '$a, $b', ' 114 110 $values = array( 0 => false, 1 => false, ); … … 123 119 124 120 if ( $values[0] && $values[1] ) { 125 $cmp = strcmp( $values[0], $values[1] ); 121 if ( "num" === "' . $type . '" ) { 122 $cmp = $values[0] - $values[1]; 123 } else { 124 $cmp = strcmp( $values[0], $values[1] ); 125 } 126 126 127 if ( 0 > $cmp ) { 127 128 $retval = -1; … … 138 139 139 140 return $items; 141 } 142 143 /** 144 * Sort an array of objects or arrays by alphabetically sorting by a specific key/property. 145 * 146 * For instance, if you have an array of WordPress post objects, you can sort 147 * them by post_name as follows: 148 * $sorted_posts = bp_alpha_sort_by_key( $posts, 'post_name' ); 149 * 150 * @since BuddyPress (1.9.0) 151 * 152 * @param array $items The items to be sorted. Its constituent items can be either associative arrays or objects. 153 * @param string|int $key The array index or property name to sort by. 154 * @return array $items The sorted array. 155 */ 156 function bp_alpha_sort_by_key( $items, $key ) { 157 return bp_sort_by_key( $items, $key, 'alpha' ); 140 158 } 141 159
Note: See TracChangeset
for help on using the changeset viewer.