Browse Source

Added ArrayHelper::sort()

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
57a6e8f333
  1. 44
      framework/util/ArrayHelper.php

44
framework/util/ArrayHelper.php

@ -218,29 +218,37 @@ class ArrayHelper
} }
/** /**
* Searches the array for a given value and returns the corresponding key if found. * Sorts a multi-dimensional array by a specific key.
* This method is similar to array_search() with the enhancement that it can also * The multi-dimensional array can be either an array of arrays or an array of objects,
* search for strings in a case-insensitive manner. * and the key can be a key name of the sub-arrays or a property name of the objects.
* @param mixed $needle the value being searched for * @param array $items the multi-dimensional array to be sorted
* @param array $haystack the array to be searched through * @param string|\Closure $key key name of the array element, or property name of the object,
* @param boolean $caseSensitive whether to perform a case-sensitive search * or an anonymous function returning the value. The anonymous function signature should be:
* @param boolean $strict whether to perform a type-strict search * `function($item)`.
* @return boolean|mixed the key of the value if it matches $needle. False if the value is not found. * @param boolean $ascending whether to sort in ascending or descending order
* @param integer $sortFlag the PHP sort flag (e.g. `SORT_REGULAR`, `SORT_NUMERIC`.)
* See [PHP manual](http://php.net/manual/en/function.sort.php) for more details.
* @return array the sorted result. Note that the array will be re-indexed with integers.
*/ */
public static function search($needle, array $haystack, $caseSensitive = true, $strict = true) public static function sort($items, $key, $ascending = true, $sortFlag = SORT_REGULAR)
{ {
if ($caseSensitive || !is_string($needle)) { if (empty($items)) {
return array_search($needle, $haystack, $strict); return $items;
} }
foreach ($haystack as $key => $value) {
if (is_string($value)) { foreach ($items as $k => $item) {
if (strcasecmp($value, $needle) === 0) { $index[$k] = static::getValue($item, $key);
return true;
} }
} elseif ($strict && $key === $value || !$strict && $key == $value) { if ($ascending) {
return true; asort($index, $sortFlag);
} else {
arsort($index, $sortFlag);
} }
$result = array();
foreach ($index as $k => $v) {
$result[] = $items[$k];
} }
return false; return $result;
} }
} }
Loading…
Cancel
Save