PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/arr.php

https://bitbucket.org/huanteng/touyou
PHP | 371 lines | 229 code | 34 blank | 108 comment | 31 complexity | 4fc837bb85351b4e5fedb9004c1f361d MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. //一维数组
  3. class arr
  4. {
  5. /*返回新数组,元素为原数组的随机一部分
  6. * $arr:原数组
  7. * $keep_count:保留的数量
  8. */
  9. function part( $arr, $keep_count )
  10. {
  11. $key = array_keys( $arr );
  12. shuffle( $key );
  13. $count = count( $arr ) - $keep_count;
  14. for( $i = 0; $i < $count; ++$i )
  15. {
  16. unset( $arr[ array_pop( $key ) ] );
  17. }
  18. return $arr;
  19. }
  20. // <editor-fold defaultstate="collapsed" desc="make_int,返回一个数组,元素由整数组成">
  21. function make_int( $min, $max )
  22. {
  23. $arr = array();
  24. for( $i = $min; $i <= $max; ++$i ) $arr[] = $i;
  25. return $arr;
  26. }
  27. // </editor-fold>
  28. // <editor-fold defaultstate="collapsed" desc="make_rand,返回一个数组,元素由随机整数组成">
  29. /*
  30. * 参数:length,返回数组的长度
  31. * min, max,每个元素下上限
  32. */
  33. function make_rand( $length, $min, $max )
  34. {
  35. $arr = array();
  36. for( $i = 0; $i < $length; ++$i ) $arr[] = mt_rand($min, $max);
  37. return $arr;
  38. }
  39. // </editor-fold>
  40. // <editor-fold defaultstate="collapsed" desc="del,删除不要的元素,返回新数组">
  41. function del( $source, $no )
  42. {
  43. foreach( $source as $k => $v )
  44. {
  45. if( in_array( $v, $no ) ) unset( $source[$k] );
  46. }
  47. return $source;
  48. }
  49. // </editor-fold>
  50. // <editor-fold defaultstate="collapsed" desc="keep,仅保留必要的元素,返回新数组">
  51. function keep( $source, $yes )
  52. {
  53. $out = array();
  54. foreach( $yes as $k )
  55. {
  56. if( isset($source[$k]) )
  57. {
  58. $out[$k] = $source[$k];
  59. }
  60. }
  61. return $out;
  62. }
  63. // </editor-fold>
  64. // <editor-fold defaultstate="collapsed" desc="json_decode,将记录集的其中一列json_decode">
  65. /* 参数:
  66. * data
  67. * key
  68. * 返回:
  69. * 转换后后记录集
  70. */
  71. function json_decode( $data, $key )
  72. {
  73. foreach( $data as $k => $v )
  74. {
  75. if( $v[ $key ] == '' ) continue;
  76. $data[ $k ][ $key ] = json_decode( $v[ $key ] );
  77. }
  78. return $data;
  79. }
  80. // </editor-fold>
  81. // <editor-fold defaultstate="collapsed" desc="rename_key,将其中一列改名">
  82. /* 参数:
  83. * data,数组
  84. * old:旧key名
  85. * new:新key名
  86. */
  87. function rename_key( $data, $old, $new )
  88. {
  89. foreach( $data as $k => $v )
  90. {
  91. $v[ $new ] = $v[ $old ];
  92. unset( $v[ $old ] );
  93. $data[$k] = $v;
  94. }
  95. return $data;
  96. }
  97. // </editor-fold>
  98. /* 如果不存在,则设置默认值
  99. * 参数:
  100. * in:原数组
  101. * default:默认值数组
  102. * 返回值:
  103. * 新数组,如果原数组中没对应值,则以对应的默认值填充
  104. */
  105. function set_default( $in, $default )
  106. {
  107. foreach( $default as $k => $v )
  108. {
  109. if( !isset($in[$k]) )
  110. {
  111. $in[$k] = $v;
  112. }
  113. }
  114. return $in;
  115. }
  116. /*
  117. * 打乱数组
  118. */
  119. function shuffle_array( $old )
  120. {
  121. $new = array();
  122. while( count( $old ) > 0 )
  123. {
  124. $key = array_rand( $old );
  125. $new[] = $old[$key];
  126. unset( $old[$key] );
  127. }
  128. return $new;
  129. }
  130. /** 对字段做html编码。(注:当字段带有双引号、<>等特殊符号时,编辑功能必须事先做html编码,否则无法正常显示)
  131. * @param $source:源记录集
  132. * @param $field:字段
  133. * 方式一:只改单个字段,可以只传入单个字段的字符串,如 html_encode( $source, 'data' )
  134. * 方式二:改多个字段,则传入字段集
  135. * 如:html_encode( $source, array( 'data1', 'data2' ) )
  136. * @return new dataset
  137. */
  138. function html_encode( $source, $field )
  139. {
  140. if( !is_array( $field ) )
  141. {
  142. $field = array( $field );
  143. }
  144. foreach( $source as $k => $v )
  145. {
  146. $source[ $k ] = htmlspecialchars( $v );
  147. }
  148. return $source;
  149. }
  150. /** 如果属性不存在,则设置。(如存在则忽略)
  151. * @param $source:原数组
  152. * @param $key:属性名
  153. * @param $value:属性值
  154. * @return 新数组
  155. */
  156. function set_if_empty( $source, $key, $value )
  157. {
  158. if( !isset( $source[ $key ] ) )
  159. {
  160. $source[ $key ] = $value;
  161. }
  162. return $source;
  163. }
  164. /** 如果属性为某值,则unset
  165. * @param $source:原数组
  166. * @param $key:属性名
  167. * @param $value:属性值
  168. * @return 新数组
  169. */
  170. function unset_if( $source, $key, $value )
  171. {
  172. if( isset( $source[ $key ] ) && $source[ $key ] == $value )
  173. {
  174. unset( $source[ $key ] );
  175. }
  176. return $source;
  177. }
  178. // 对给定二维数组按照指定的键值进行排序
  179. function sort($arr,$keys,$type='asc'){
  180. $keysvalue = $new_array = array();
  181. foreach ($arr as $k=>$v){
  182. $keysvalue[$k] = $v[$keys];
  183. }
  184. if($type == 'asc'){
  185. asort($keysvalue);
  186. }else{
  187. arsort($keysvalue);
  188. }
  189. reset($keysvalue);
  190. foreach ($keysvalue as $k=>$v){
  191. $new_array[$k] = $arr[$k];
  192. }
  193. return $new_array;
  194. }
  195. /** 对 source 进行分组,每 group 一组,组内的数据打乱。
  196. * @param $source:打乱前的数组
  197. * @param $group:数字,每组多少个?最后一组可能少于它
  198. * @return mixed:打乱后的数组
  199. */
  200. function group_sort( $source, $group )
  201. {
  202. $key = array_keys( $source );
  203. $key_count = count( $key );
  204. // 进一取整、舍弃取整
  205. $more = ceil( $key_count / $group );
  206. $less = floor( $key_count / $group );
  207. // 乱序数组长度及具体数组
  208. $len = $group;
  209. $rand = $this->make_int( 0, $len - 1 );
  210. $out = array();
  211. for( $i = 0; $i < $more; ++$i )
  212. {
  213. // 乱序数组个数,平时是group个,最后一批是余数
  214. if( $i == $more - 1 && $more > $less )
  215. {
  216. $len = $key_count - $less * $group;
  217. $rand = $this->make_int( 0, $len - 1 );
  218. }
  219. $rand = $this->shuffle_array( $rand );
  220. for( $j = 0; $j < $len; ++$j )
  221. {
  222. // 当前元素名
  223. $k = $key[ $i * $group + $rand[ $j ] ];
  224. $out[ $k ] = $source[ $k ];
  225. }
  226. }
  227. return $out;
  228. }
  229. /**
  230. * 根据value是1或0,分别显示是或否。为简便操作
  231. * 返回值:
  232. * 不带参数时,返回{ 0:否,1:是}
  233. * 带参数时,返回是或否
  234. */
  235. function yes_no_dict( $value = -1 )
  236. {
  237. $data = array( 1 => '是', 0 => '否' );
  238. if( $value == -1 )
  239. {
  240. return $data;
  241. }
  242. else
  243. {
  244. return $value == 1 ? '是' : '否';
  245. }
  246. }
  247. /** 将数组value串接起来,返回字符串。(注:本方法和base::implode2类似,但可设置前后辍不相同
  248. * @param $data:数组
  249. * @param string $pre:每个value前辍
  250. * @param string $end:每个value后辍
  251. * @param string $join:value之间用什么串接
  252. * @return string:
  253. */
  254. function implode( $data, $pre = "'", $end = "'", $join = ',')
  255. {
  256. $s = '';
  257. foreach( $data as $v )
  258. {
  259. if( is_array( $v ) )
  260. {
  261. $s .= $this->implode( $v, $pre, $end, $join );
  262. }else
  263. {
  264. $s .= $pre . $v . $end . $join;
  265. }
  266. }
  267. if( $s != '' && $join != '' )
  268. {
  269. $s = substr($s, 0, -strlen( $join ) );
  270. }
  271. return $s;
  272. }
  273. /** 过滤子value里面的html代码
  274. * @param $data:数组
  275. * @return 过滤后的新数组
  276. */
  277. function strip_tags( $data )
  278. {
  279. foreach( $data as &$v )
  280. {
  281. $v = is_array( $v ) ? $this->strip_tags( $v ) : strip_tags( $v );
  282. }
  283. return $data;
  284. }
  285. /** 将子value前后的空格过滤
  286. * @param $data:数组
  287. * @return 过滤后的新数组
  288. */
  289. function trim( $data )
  290. {
  291. foreach( $data as &$v )
  292. {
  293. $v = is_array( $v ) ? $this->trim( $v ) : trim( $v );
  294. }
  295. return $data;
  296. }
  297. /** // 注:不知为何要在此加addslashes,但如果不加的话,当json含有单引号时,会出现错误
  298. * @param $json
  299. * @return string
  300. */
  301. function php_json_encode( $json )
  302. {
  303. return addslashes( json_encode( $json ) );
  304. }
  305. /** 往数组的某个key追回元素。(如果该key不是数组,事先定义为数组)
  306. * @param $source
  307. * @param $key
  308. * @param $value
  309. * @return mixed
  310. */
  311. function append( $source, $key, $value )
  312. {
  313. if( !isset( $source[ $key ] ) )
  314. {
  315. $source[ $key ] = array();
  316. }
  317. $source[ $key ][] = $value;
  318. return $source;
  319. }
  320. /** 删除数组中值为 $value 的元素。(可能0个、1个、多个)
  321. * @param $source
  322. * @param $value
  323. * @return 新数组
  324. */
  325. function del_value( $source, $value )
  326. {
  327. foreach( $source as $k => $v )
  328. {
  329. if( $v == $value )
  330. {
  331. unset( $source[ $k ] );
  332. }
  333. }
  334. return $source;
  335. }
  336. }
  337. ?>