Created
December 7, 2015 08:45
-
-
Save forecho/ed417514643ca62ee87b to your computer and use it in GitHub Desktop.
传一个数组,返回一个最小值和最大值。应用场景是获取商品SKU的价格区间
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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