STL.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of STL
  9. *
  10. * @author jgao
  11. */
  12. class STL {
  13. //put your code here
  14. /**
  15. * 字典是否存在某元素
  16. * @param type $dict
  17. * @param type $property
  18. */
  19. static function dictContainsKey($dict, $property){
  20. return property_exists($dict, "".$property);
  21. }
  22. /**
  23. * 移除字典元素
  24. * @param type $dict
  25. * @param type $property
  26. */
  27. static function dictRemove($dict, $property){
  28. unset($dict->$property);
  29. }
  30. /**
  31. * 添加字典元素
  32. * @param type $dict
  33. * @param type $property
  34. * @param type $value
  35. */
  36. static function dictInsert($dict, $property, $value){
  37. $dict->$property = $value;
  38. }
  39. /**
  40. * 获取字典值
  41. * @param type $dict
  42. * @param type $property
  43. */
  44. static function dictValue($dict, $property, $default=null){
  45. if(property_exists($dict, "".$property)){
  46. return $dict->$property;
  47. }else{
  48. return $default;
  49. }
  50. }
  51. /**
  52. * 获取字典全部属性
  53. * @param type $dict
  54. * @return type
  55. */
  56. static function dictPropertys($dict){
  57. $propertys = array();
  58. foreach($dict as $key => $val){
  59. $propertys[] = $key;
  60. }
  61. return $propertys;
  62. }
  63. /**
  64. * 获取字典全部值
  65. * @param type $dict
  66. */
  67. static function dictValues($dict){
  68. $values = array();
  69. foreach ($dict as $key => $val){
  70. $values[] = $val;
  71. }
  72. return $values;
  73. }
  74. /**
  75. * 随机字典任意属性名
  76. * @param type $dict
  77. */
  78. static function dictRandom($dict){
  79. $kvs = array();
  80. $key = self::arrayRandom(self::dictPropertys($dict));
  81. $kvs[$key] = $dict->$key;
  82. return $kvs;
  83. }
  84. /**
  85. * 随机字典任一属性值
  86. * @param type $dict
  87. * @return type
  88. */
  89. static function dictRandoms($dict, $num){
  90. $kvs = array();
  91. $keys = self::arrayRandoms(self::dictPropertys($dict),$num);
  92. foreach ($keys as $key){
  93. $kvs[$key] = $dict->$key;
  94. }
  95. return $kvs;
  96. }
  97. /**
  98. * 字典转数组
  99. * @param type $dict 目标字典
  100. * @param type $mod 0.值转换;1.键转换;2.部分属性转换
  101. * @param type $propertyList 属性列表【数组】,仅$mod==2时生效
  102. * @return type
  103. */
  104. static function dictToArray($dict, $mod=0, $propertyList=null){
  105. $array = array();
  106. foreach($dict as $key=>$val){
  107. if($mod == 0){ // 值转数组
  108. $array[] = $val;
  109. }else if($mod == 1){ // key转数组
  110. $array[] = $key;
  111. }else{ // 单属性转数组
  112. $item = new stdClass();
  113. foreach($propertyList as $property){
  114. if(self::dictHasProperty($dict, $property)){
  115. $item->$property = $dict->$property;
  116. }
  117. }
  118. $array[] = $item;
  119. }
  120. }
  121. return $array;
  122. }
  123. /**
  124. * 数组新增
  125. * @param array $array
  126. * @param type $item
  127. */
  128. static function arrayPush(&$array,$item){
  129. $array[] = $item;
  130. }
  131. /**
  132. * 获取数组元素
  133. * @param type $array
  134. * @param type $index
  135. */
  136. static function arrayGet(&$array, $index){
  137. return $array[$index];
  138. }
  139. /**
  140. * 设置数组元素
  141. * @param type $array
  142. * @param type $index
  143. * @param type $item
  144. * @return type
  145. */
  146. static function arraySet(&$array, $index, $item){
  147. if($index >= count($array)){
  148. $array[] = $item;
  149. return;
  150. }
  151. $array[$index] = $item;
  152. }
  153. /**
  154. * 数组插入
  155. * @param type $array
  156. * @param type $item
  157. * @param type $index
  158. */
  159. static function arrayInsert(&$array,$index,$item){
  160. if($index >= count($array)){
  161. $array[] = $item;
  162. return;
  163. }
  164. $fore = ($index==0)?array():array_splice($array,0,$index);
  165. $fore[] = $item;
  166. $array = array_merge($fore,$array);
  167. }
  168. /**
  169. * 数组移除
  170. * @param type $array
  171. * @param type $item
  172. */
  173. static function arrayRemove(&$array,$item){
  174. array_splice($array,self::arrayIndexOf($array,$item),1);
  175. }
  176. /**
  177. * 数组移除
  178. * @param type $array
  179. * @param type $index
  180. */
  181. static function arrayRemoveAt(&$array,$index){
  182. array_splice($array,$index,1);
  183. }
  184. /**
  185. * 数组克隆
  186. * @param type $array
  187. */
  188. static function arrayClone($array){
  189. return array_slice($array, 0);
  190. }
  191. /**
  192. * 数组清空
  193. * @param type $array
  194. */
  195. static function arrayClear(&$array){
  196. array_splice($array,0);
  197. }
  198. /**
  199. * 元素是否位于某数组中
  200. * @param type $array
  201. * @param type $item
  202. * @return type
  203. */
  204. static function arrayContains($array,$item){
  205. return in_array($item, $array);
  206. }
  207. /**
  208. * 查找元素在数组中的位置
  209. * @param type $array
  210. * @param type $item
  211. * @return type
  212. */
  213. static function arrayIndexOf($array, $item){
  214. $indexs = array_keys($array, $item);
  215. if(count($indexs)<=0) return -1;
  216. return $indexs[0];
  217. }
  218. /**
  219. * 数组去重
  220. * @param type $array
  221. */
  222. static function arrayUnique(&$array){
  223. $array = array_values(array_unique($array));
  224. return $array;
  225. }
  226. /**
  227. * 数组升序排列
  228. * @param type $array
  229. */
  230. static function arraySort(&$array){
  231. return sort($array);
  232. }
  233. /**
  234. * 数组降序排列
  235. * @param type $array
  236. */
  237. static function arrayRsort(&$array){
  238. return rsort($array);
  239. }
  240. /**
  241. * 统计数组元素数或者对象属性个数
  242. * @param type $var
  243. * @return type
  244. */
  245. static function count($var){
  246. return count((array)$var);
  247. }
  248. /**
  249. * 数组随机取样
  250. * @param type $array
  251. * @return type
  252. */
  253. static function arrayRandom($array){
  254. $iCount = self::count($array);
  255. $ranIndex = rand(0, $iCount - 1);
  256. return $array[$ranIndex];
  257. }
  258. /**
  259. * 数组随机取样
  260. * @param type $array 目标数组
  261. * @param type $num 取样数目
  262. */
  263. static function arrayRandoms($array,$num){
  264. $newarray = array();
  265. if(self::count($array)<$num){
  266. $num = self::count($array);
  267. }
  268. $oldarray = self::arrayClone($array);
  269. do{
  270. $iCount = self::count($oldarray);
  271. if($iCount<=0){
  272. break;
  273. }else{
  274. $ranIndex = rand(0, $iCount - 1);
  275. $newarray[] = $oldarray[$ranIndex];
  276. self::arrayRemoveAt($oldarray, $ranIndex);
  277. }
  278. }while(self::count($newarray)<$num);
  279. return $newarray;
  280. }
  281. }