123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of STL
- *
- * @author jgao
- */
- class STL {
- //put your code here
-
- /**
- * 字典是否存在某元素
- * @param type $dict
- * @param type $property
- */
- static function dictContainsKey($dict, $property){
- return property_exists($dict, "".$property);
- }
-
- /**
- * 移除字典元素
- * @param type $dict
- * @param type $property
- */
- static function dictRemove($dict, $property){
- unset($dict->$property);
- }
-
- /**
- * 添加字典元素
- * @param type $dict
- * @param type $property
- * @param type $value
- */
- static function dictInsert($dict, $property, $value){
- $dict->$property = $value;
- }
-
- /**
- * 获取字典值
- * @param type $dict
- * @param type $property
- */
- static function dictValue($dict, $property, $default=null){
- if(property_exists($dict, "".$property)){
- return $dict->$property;
- }else{
- return $default;
- }
- }
- /**
- * 获取字典全部属性
- * @param type $dict
- * @return type
- */
- static function dictPropertys($dict){
- $propertys = array();
- foreach($dict as $key => $val){
- $propertys[] = $key;
- }
- return $propertys;
- }
-
- /**
- * 获取字典全部值
- * @param type $dict
- */
- static function dictValues($dict){
- $values = array();
- foreach ($dict as $key => $val){
- $values[] = $val;
- }
- return $values;
- }
- /**
- * 随机字典任意属性名
- * @param type $dict
- */
- static function dictRandom($dict){
- $kvs = array();
- $key = self::arrayRandom(self::dictPropertys($dict));
- $kvs[$key] = $dict->$key;
- return $kvs;
- }
-
- /**
- * 随机字典任一属性值
- * @param type $dict
- * @return type
- */
- static function dictRandoms($dict, $num){
- $kvs = array();
- $keys = self::arrayRandoms(self::dictPropertys($dict),$num);
- foreach ($keys as $key){
- $kvs[$key] = $dict->$key;
- }
- return $kvs;
- }
-
- /**
- * 字典转数组
- * @param type $dict 目标字典
- * @param type $mod 0.值转换;1.键转换;2.部分属性转换
- * @param type $propertyList 属性列表【数组】,仅$mod==2时生效
- * @return type
- */
- static function dictToArray($dict, $mod=0, $propertyList=null){
- $array = array();
- foreach($dict as $key=>$val){
- if($mod == 0){ // 值转数组
- $array[] = $val;
- }else if($mod == 1){ // key转数组
- $array[] = $key;
- }else{ // 单属性转数组
- $item = new stdClass();
- foreach($propertyList as $property){
- if(self::dictHasProperty($dict, $property)){
- $item->$property = $dict->$property;
- }
- }
- $array[] = $item;
- }
- }
- return $array;
- }
-
- /**
- * 数组新增
- * @param array $array
- * @param type $item
- */
- static function arrayPush(&$array,$item){
- $array[] = $item;
- }
-
- /**
- * 获取数组元素
- * @param type $array
- * @param type $index
- */
- static function arrayGet(&$array, $index){
- return $array[$index];
- }
-
- /**
- * 设置数组元素
- * @param type $array
- * @param type $index
- * @param type $item
- * @return type
- */
- static function arraySet(&$array, $index, $item){
- if($index >= count($array)){
- $array[] = $item;
- return;
- }
- $array[$index] = $item;
- }
- /**
- * 数组插入
- * @param type $array
- * @param type $item
- * @param type $index
- */
- static function arrayInsert(&$array,$index,$item){
- if($index >= count($array)){
- $array[] = $item;
- return;
- }
- $fore = ($index==0)?array():array_splice($array,0,$index);
- $fore[] = $item;
- $array = array_merge($fore,$array);
- }
-
- /**
- * 数组移除
- * @param type $array
- * @param type $item
- */
- static function arrayRemove(&$array,$item){
- array_splice($array,self::arrayIndexOf($array,$item),1);
- }
-
- /**
- * 数组移除
- * @param type $array
- * @param type $index
- */
- static function arrayRemoveAt(&$array,$index){
- array_splice($array,$index,1);
- }
-
- /**
- * 数组克隆
- * @param type $array
- */
- static function arrayClone($array){
- return array_slice($array, 0);
- }
-
- /**
- * 数组清空
- * @param type $array
- */
- static function arrayClear(&$array){
- array_splice($array,0);
- }
-
- /**
- * 元素是否位于某数组中
- * @param type $array
- * @param type $item
- * @return type
- */
- static function arrayContains($array,$item){
- return in_array($item, $array);
- }
-
- /**
- * 查找元素在数组中的位置
- * @param type $array
- * @param type $item
- * @return type
- */
- static function arrayIndexOf($array, $item){
- $indexs = array_keys($array, $item);
- if(count($indexs)<=0) return -1;
- return $indexs[0];
- }
-
- /**
- * 数组去重
- * @param type $array
- */
- static function arrayUnique(&$array){
- $array = array_values(array_unique($array));
- return $array;
- }
-
- /**
- * 数组升序排列
- * @param type $array
- */
- static function arraySort(&$array){
- return sort($array);
- }
-
- /**
- * 数组降序排列
- * @param type $array
- */
- static function arrayRsort(&$array){
- return rsort($array);
- }
- /**
- * 统计数组元素数或者对象属性个数
- * @param type $var
- * @return type
- */
- static function count($var){
- return count((array)$var);
- }
-
- /**
- * 数组随机取样
- * @param type $array
- * @return type
- */
- static function arrayRandom($array){
- $iCount = self::count($array);
- $ranIndex = rand(0, $iCount - 1);
- return $array[$ranIndex];
- }
-
- /**
- * 数组随机取样
- * @param type $array 目标数组
- * @param type $num 取样数目
- */
- static function arrayRandoms($array,$num){
- $newarray = array();
- if(self::count($array)<$num){
- $num = self::count($array);
- }
- $oldarray = self::arrayClone($array);
- do{
- $iCount = self::count($oldarray);
- if($iCount<=0){
- break;
- }else{
- $ranIndex = rand(0, $iCount - 1);
- $newarray[] = $oldarray[$ranIndex];
- self::arrayRemoveAt($oldarray, $ranIndex);
- }
- }while(self::count($newarray)<$num);
- return $newarray;
- }
- }
|