StlUtil.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of StlUtil
  5. *
  6. * @author jgao
  7. */
  8. class StlUtil {
  9. //put your code here
  10. /**
  11. * 字典是否存在某元素
  12. * @param associat_array $dict
  13. * @param type $property
  14. */
  15. public static function dictHasProperty($dict, $property) {
  16. return property_exists($dict, "" . $property);
  17. }
  18. /**
  19. * 移除字典元素
  20. * @param associat_array $dict
  21. * @param type $property
  22. */
  23. public static function dictRemove($dict, $property) {
  24. unset($dict->$property);
  25. }
  26. /**
  27. * 获取字典全部属性
  28. * @param associat_array $dict
  29. * @return array
  30. */
  31. public static function dictPropertys($dict) {
  32. $propertys = array();
  33. foreach ($dict as $key => $val) {
  34. $propertys[] = $key;
  35. }
  36. return $propertys;
  37. }
  38. /**
  39. * 随机字典任意属性名
  40. * @param associat_array $dict
  41. */
  42. public static function dictPropertyRandom($dict) {
  43. return self::arrayRandom(self::dictPropertys($dict));
  44. }
  45. /**
  46. * 随机字典任一属性值
  47. * @param type $dict
  48. * @return obj
  49. */
  50. public static function dictRandom($dict) {
  51. $key = self::arrayRandom(self::dictPropertys($dict));
  52. return $dict->$key;
  53. }
  54. /**
  55. * 字典转数组
  56. * @param associat_array $dict 目标字典
  57. * @param type $mod 0.值转换;1.键转换;2.部分属性转换
  58. * @param type $propertyList 属性列表【数组】,仅$mod==2时生效
  59. * @return type
  60. */
  61. public static function dictToArray($dict, $mod = 0, $propertyList = null) {
  62. $array = array();
  63. foreach ($dict as $key => $val) {
  64. if ($mod == 0) { // 值转数组
  65. $array[] = $val;
  66. } elseif ($mod == 1) { // key转数组
  67. $array[] = $key;
  68. } else { // 单属性转数组
  69. $item = new \stdClass();
  70. foreach ($propertyList as $property) {
  71. if (self::dictHasProperty($dict, $property)) {
  72. $item->$property = $dict->$property;
  73. }
  74. }
  75. $array[] = $item;
  76. }
  77. }
  78. return $array;
  79. }
  80. /**
  81. * 创建数组,并将传入的参数压入数组
  82. * @param 可选...args
  83. */
  84. public static function arrayInit(/* $arg1, $arg2...$argN */) {
  85. $array = array();
  86. $items = func_get_args();
  87. foreach ($items as $item) {
  88. $array[] = $item;
  89. }
  90. return $array;
  91. }
  92. /**
  93. * 数组新增
  94. * @param array $array
  95. * @param type $item
  96. */
  97. public static function arrayPush(&$array, $item) {
  98. $array[] = $item;
  99. }
  100. /**
  101. * 获取数组元素
  102. * @param type $array
  103. * @param type $index
  104. */
  105. public static function arrayGet(&$array, $index) {
  106. return $array[$index];
  107. }
  108. /**
  109. * 设置数组元素
  110. * @param type $array
  111. * @param type $index
  112. * @param type $item
  113. * @return type
  114. */
  115. public static function arraySet(&$array, $index, $item) {
  116. if ($index >= count($array)) {
  117. $array[] = $item;
  118. return;
  119. }
  120. $array[$index] = $item;
  121. }
  122. /**
  123. * 数组插入
  124. * @param type $array
  125. * @param type $item
  126. * @param type $index
  127. */
  128. public static function arrayInsert(&$array, $index, $item) {
  129. $fore = ($index == 0) ? array() : array_splice($array, 0, $index);
  130. $fore[] = $item;
  131. $array = array_merge($fore, $array);
  132. }
  133. /**
  134. * 数组移除
  135. * @param array $array 数组
  136. * @param any $item 元素
  137. */
  138. public static function arrayRemove(&$array, $item) {
  139. $index = self::arrayIndexOf($array, $item);
  140. if ($index == -1) {
  141. } else {
  142. array_splice($array, $index, 1);
  143. }
  144. }
  145. /**
  146. * 数组移除
  147. * @param array $array
  148. * @param int $index
  149. */
  150. public static function arrayRemoveAt(&$array, $index) {
  151. array_splice($array, $index, 1);
  152. }
  153. /**
  154. * 数组克隆
  155. * @param array $array
  156. */
  157. public static function arrayClone($array) {
  158. return array_slice($array, 0);
  159. }
  160. /**
  161. * 数组清空
  162. * @param array $array
  163. */
  164. public static function arrayClear(&$array) {
  165. array_splice($array, 0);
  166. }
  167. /**
  168. * 元素是否位于某数组中
  169. * @param array $array
  170. * @param any $item
  171. * @return type
  172. */
  173. public static function arrayContains($array, $item) {
  174. return in_array($item, $array);
  175. }
  176. /**
  177. * 查找元素在数组中的位置
  178. * @param array $array
  179. * @param any $item
  180. * @return int
  181. */
  182. public static function arrayIndexOf($array, $item) {
  183. $indexs = array_keys($array, $item);
  184. if (count($indexs) > 0) {
  185. return $indexs[0];
  186. } else {
  187. return -1;
  188. }
  189. }
  190. /**
  191. * 数组去重
  192. * @param type $array
  193. */
  194. public static function arrayUnique(&$array) {
  195. $array = array_values(array_unique($array));
  196. }
  197. /**
  198. * 模仿as3中的sortby函数
  199. * @param type $arr
  200. * @param type $propertyName_sortBy
  201. */
  202. public static function arraySortBy($arr, $propertyName_sortBy) {
  203. throw new ErrorException("funciton not finish!");
  204. $a = function ($a, $b) {
  205. if ($a->id > $b->id) {
  206. return 1;
  207. } elseif ($a->id == $b->id) {
  208. return 0;
  209. } else {
  210. return -1;
  211. }
  212. };
  213. }
  214. /**
  215. * 在数组中查找第一个符合条件的值
  216. * @param mixed $array
  217. * @param bool $closure 是否符合条件的判断方法
  218. * @return type
  219. */
  220. public static function arrayFind($array, $closure) {
  221. foreach ($array as $key => $value) {
  222. if ($closure($value, $key)) {
  223. return $value;
  224. }
  225. }
  226. return null;
  227. }
  228. /**
  229. * 辅助方法,将关联数组转换成class
  230. * @param type $assocArray
  231. * @return \stdClass
  232. */
  233. public static function array2class($assocArray) {
  234. $ret = new \stdClass();
  235. foreach ($assocArray as $p => $v) {
  236. $ret->$p = $v;
  237. }
  238. return $ret;
  239. }
  240. /**
  241. * 辅助方法,将数组转换成对象结构方便使用箭头操作
  242. * @param array $arr [{'kname':vallue,some:value},{'kname':vallue,some:value}...]
  243. * @param string $kName 作为key的字段的名称
  244. * @return \stdClass
  245. */
  246. public static function array2dict($arr, $kName) {
  247. $ret = new \stdClass();
  248. foreach ($arr as $item) {
  249. $k = $item->$kName;
  250. if ($k) {
  251. $ret->$k = $item;
  252. }
  253. }
  254. return $ret;
  255. }
  256. /**
  257. * 统计数组元素数或者对象属性个数
  258. * @param type $var
  259. * @return type
  260. */
  261. public static function count($var) {
  262. return count((array) $var);
  263. }
  264. }
  265. /**
  266. * 函数式写法
  267. * @param type $arr
  268. * @param type $item
  269. * @return type
  270. */
  271. function array_contains($arr, $item) {
  272. return StlUtil::arrayContains($arr, $item);
  273. }
  274. //\
  275. /**
  276. * 关联数组, json_encode之后转为{}
  277. */
  278. class asoc_array {
  279. }