Skip to content

Instantly share code, notes, and snippets.

@forecho
Created December 7, 2015 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forecho/ed417514643ca62ee87b to your computer and use it in GitHub Desktop.
Save forecho/ed417514643ca62ee87b to your computer and use it in GitHub Desktop.
传一个数组,返回一个最小值和最大值。应用场景是获取商品SKU的价格区间
// TFuncProduct::getInterval(array(
// 10 => array('title' => 'a', 'size' => 1),
// 20 => array('title' => 'b', 'size' => 2),
// 30 => array('title' => 'c', 'size' => 3),
// ),
// 'size'
// );
// 输出 array(1, 3)
/**
* 区间 小-大
* @param $collection
* @param string $field
* @return array
*/
public static function getInterval($collection, $field)
{
$values = array_map(function ($item) use ($field) {
return $item[$field];
}, $collection);
$keys = array_flip($values);
return array(
$collection[$keys[min($values)]][$field],
$collection[$keys[max($values)]][$field]
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment