Arr.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. /**
  3. * The Feeler framework, licensed under Mit license
  4. * Author: Rick Guo
  5. */
  6. class Arr{
  7. //Array of keys to delete and restore to incrementing key
  8. public static function tidy(&$array = array()){
  9. if($array){
  10. array_unshift($array, array());
  11. array_splice($array, 0, 1);
  12. return true;
  13. }
  14. return false;
  15. }
  16. //Sort the 2D array by the 2nd dimension value, according is the 2nd dimension key
  17. public static function sortByField(&$array, $field, $order = "ASC", $type = "SORT_NATURAL", $keepKey = false){
  18. if(!is_array($array) || !$array || !is_string($field) || trim($field) == "")
  19. return false;
  20. $arr1 = $arr2 = array();
  21. foreach($array as $key => $val){
  22. if(isset($val[$field])) $arr1[$key] = $val[$field];
  23. }
  24. if($type == "SORT_NATURAL"){
  25. if($order == "ASC"){
  26. natsort($arr1);
  27. }
  28. else if($order == "DESC"){
  29. natsort($arr1);
  30. $arr1 = array_reverse($arr1, true);
  31. }
  32. else
  33. return false;
  34. }
  35. else{
  36. switch($type){
  37. case "SORT_NUMERIC":
  38. $type = SORT_NUMERIC;
  39. break;
  40. case "SORT_REGULAR":
  41. $type = SORT_REGULAR;
  42. break;
  43. case "SORT_STRING":
  44. $type = SORT_STRING;
  45. break;
  46. case "SORT_LOCALE_STRING":
  47. $type = SORT_LOCALE_STRING;
  48. break;
  49. default:
  50. return false;
  51. break;
  52. }
  53. if($order === "ASC")
  54. asort($arr1, $type);
  55. else if($order === "DESC")
  56. arsort($arr1, $type);
  57. else
  58. return false;
  59. }
  60. if($keepKey){
  61. foreach($arr1 as $key => $val){
  62. $arr2[$key] = $array[$key];
  63. }
  64. }
  65. else{
  66. foreach($arr1 as $key => $val){
  67. $arr2[] = $array[$key];
  68. }
  69. }
  70. $array = $arr2;
  71. return true;
  72. }
  73. public static function sort(&$array, $order = "ASC", $type = "SORT_NATURAL", $keepKey = true){
  74. if(!is_array($array) || !$array)
  75. return false;
  76. if($type == "SORT_NATURAL"){
  77. if($order == "ASC"){
  78. natsort($array);
  79. }
  80. else if($order == "DESC"){
  81. natsort($array);
  82. $array = array_reverse($array, true);
  83. }
  84. else
  85. return false;
  86. }
  87. else{
  88. switch($type){
  89. case "SORT_NUMERIC":
  90. $type = SORT_NUMERIC;
  91. break;
  92. case "SORT_REGULAR":
  93. $type = SORT_REGULAR;
  94. break;
  95. case "SORT_STRING":
  96. $type = SORT_STRING;
  97. break;
  98. case "SORT_LOCALE_STRING":
  99. $type = SORT_LOCALE_STRING;
  100. break;
  101. default:
  102. return false;
  103. break;
  104. }
  105. if($order === "ASC")
  106. asort($array, $type);
  107. else if($order === "DESC")
  108. arsort($array, $type);
  109. else
  110. return false;
  111. }
  112. if(!$keepKey)
  113. self::tidy($array);
  114. return true;
  115. }
  116. public static function ksort(&$array, $order = "ASC", $type = "SORT_NATURAL"){
  117. if(!is_array($array) || !$array)
  118. return false;
  119. if($type == "SORT_NATURAL"){
  120. $keys = array_keys($array);
  121. if($order == "ASC"){
  122. natsort($keys);
  123. }
  124. else if($order == "DESC"){
  125. natsort($keys);
  126. $keys = array_reverse($keys, true);
  127. }
  128. else
  129. return false;
  130. $arr1 = array();
  131. foreach($keys as $key){
  132. foreach($array as $k => $v){
  133. if($key === $k){
  134. $arr1[$key] = $v;
  135. }
  136. }
  137. }
  138. $array = $arr1;
  139. return true;
  140. }
  141. else{
  142. switch($type){
  143. case "SORT_NUMERIC":
  144. $type = SORT_NUMERIC;
  145. break;
  146. case "SORT_REGULAR":
  147. $type = SORT_REGULAR;
  148. break;
  149. case "SORT_STRING":
  150. $type = SORT_STRING;
  151. break;
  152. case "SORT_LOCALE_STRING":
  153. $type = SORT_LOCALE_STRING;
  154. break;
  155. default:
  156. return false;
  157. break;
  158. }
  159. if($order === "ASC")
  160. ksort($array, $type);
  161. else if($order === "DESC")
  162. krsort($array, $type);
  163. else
  164. return false;
  165. return true;
  166. }
  167. return false;
  168. }
  169. public static function slice($array, $offset, $length = null, $keepKey = true){
  170. return array_slice($array, $offset, $length, $keepKey);
  171. }
  172. public static function makeOrderField(&$array, $field, $start = 1){
  173. if(!self::isAvailable($array))
  174. return false;
  175. if(!$field)
  176. return false;
  177. if(!is_int($start))
  178. return false;
  179. foreach($array as $key => $val){
  180. $array[$key][$field] = $start;
  181. $start++;
  182. }
  183. return true;
  184. }
  185. //merge multi array and make the values different
  186. public static function merge($array){
  187. $params = func_get_args();
  188. if(isset($params[1])){
  189. self::tidy($array);
  190. unset($params[0]);
  191. foreach($params as $param){
  192. if(is_array($param)){
  193. foreach($param as $val){
  194. if(is_string($val))
  195. $val = trim($val);
  196. if(!in_array($val, $array))
  197. $array[] = $val;
  198. }
  199. }
  200. }
  201. }
  202. return $array;
  203. }
  204. //merge multi array and make the values different
  205. public static function plus($array){
  206. $params = func_get_args();
  207. if(isset($params[1])){
  208. self::tidy($array);
  209. unset($params[0]);
  210. foreach($params as $param){
  211. if(is_array($param)){
  212. foreach($param as $key => $val){
  213. if(is_string($val))
  214. $val = trim($val);
  215. if(!array_key_exists($key, $array))
  216. $array[$key] = $val;
  217. }
  218. }
  219. }
  220. }
  221. return $array;
  222. }
  223. //merge multi array and make the values different
  224. public static function pack($array){
  225. $params = func_get_args();
  226. if(isset($params[1])){
  227. unset($params[0]);
  228. foreach($params as $param){
  229. if(is_array($param)){
  230. foreach($param as $key => $val){
  231. if(is_string($val))
  232. $val = trim($val);
  233. if(!empty($key) || $key === 0)
  234. $array[$key] = $val;
  235. }
  236. }
  237. }
  238. }
  239. return $array;
  240. }
  241. //merge multi array and don't check the values are unique or not
  242. public static function mergeAll($array){
  243. $params = func_get_args();
  244. if(isset($params[1])){
  245. self::tidy($array);
  246. unset($params[0]);
  247. foreach($params as $param){
  248. foreach($param as $val){
  249. if(is_string($val))
  250. $val = trim($val);
  251. $array[] = $val;
  252. }
  253. }
  254. }
  255. return $array;
  256. }
  257. public static function unique($array){
  258. if(is_array($array) && $array){
  259. $newArray = array();
  260. foreach($array as $key => $val){
  261. if(is_string($val))
  262. $val = trim($val);
  263. if(!in_array($val, $newArray))
  264. $newArray[$key] = $val;
  265. }
  266. return $newArray;
  267. }
  268. return $array;
  269. }
  270. //For large data analysis of 2D data merge stack numbers other elements only
  271. public static function mergeByKey($array){
  272. $params = func_get_args();
  273. if(isset($params[1])){
  274. unset($params[0]);
  275. if(!$array)
  276. $array = array();
  277. foreach($params as $param){
  278. foreach($param as $key => $arr){
  279. if(!$arr || !is_array($arr))
  280. continue;
  281. if(!isset($array[$key]))
  282. $array[$key] = $arr;
  283. else{
  284. foreach($arr as $k => $v){
  285. if(isset($array[$key][$k])){
  286. if(is_numeric($array[$key][$k]) && is_numeric($v)){
  287. $array[$key][$k] += $v;
  288. }
  289. else if(is_array($array[$key][$k]) && is_array($v)){
  290. $array[$key][$k] = self::merge($array[$key][$k], $v);
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. return $array;
  299. }
  300. //count the deepest dimension of the array
  301. public static function countDimension($array){
  302. $dimension = array();
  303. if(is_array($array)){
  304. foreach($array as $key => $arr){
  305. if(!isset($dimension[$key]))
  306. $dimension[$key] = 0;
  307. if(is_array($arr)){
  308. $dimension[$key] += self::countDimension($arr);
  309. }
  310. }
  311. arsort($dimension, SORT_NUMERIC);
  312. $dimension = current($dimension) + 1;
  313. }
  314. else
  315. $dimension = 1;
  316. return $dimension;
  317. }
  318. //array to object conversion
  319. public static function toObj($arr){
  320. if(gettype($arr) != "array") return null;
  321. foreach($arr as $k => $v){
  322. if(gettype($v) == "array" || getType($v) == "object")
  323. $arr[$k] = (object)self::toObj($v);
  324. }
  325. return (object)$arr;
  326. }
  327. //checkout the values in first array, but not in the other arrays
  328. public static function diff($array){
  329. if(!is_array($array))
  330. return $array;
  331. $params = func_get_args();
  332. $diff = array();
  333. if(isset($params[1])){
  334. unset($params[0]);
  335. foreach($params as $arr){
  336. if(!is_array($arr))
  337. continue;
  338. foreach($array as $key => $val){
  339. if(!in_array($val, $arr) && !in_array($val, $diff))
  340. $diff[] = $val;
  341. }
  342. }
  343. }
  344. return $diff;
  345. }
  346. //checkout the same values between the first array and the other arrays
  347. public static function intersect($array){
  348. if(!is_array($array))
  349. return $array;
  350. $params = func_get_args();
  351. $same = array();
  352. if(isset($params[1])){
  353. unset($params[0]);
  354. foreach($params as $arr){
  355. if(!is_array($arr))
  356. continue;
  357. foreach($arr as $key => $val){
  358. if(in_array($val, $array) && !in_array($val, $same))
  359. $same[] = $val;
  360. }
  361. }
  362. }
  363. return $same;
  364. }
  365. public static function isAvailable($arr, $key = null){
  366. if(!is_array($arr))
  367. return false;
  368. if(!$key)
  369. return $arr ? true : false;
  370. if(is_array($key)){
  371. foreach($key as $k){
  372. if($matches = Regex::matchKeyWithType($k)){
  373. if(!Type::verify($matches[1], $matches[0]))
  374. return false;
  375. }
  376. if(!isset($arr[$k]))
  377. return false;
  378. }
  379. }
  380. else{
  381. if($matches = Regex::matchKeyWithType($key)){
  382. if(!Type::verify($matches[1], $matches[0]))
  383. return false;
  384. }
  385. if(!isset($arr[$key]))
  386. return false;
  387. }
  388. return true;
  389. }
  390. public static function keyExists($arr, $key){
  391. return isset($arr[$key]) ? true : false;
  392. }
  393. public static function current($arr){
  394. if(self::isAvailable($arr))
  395. $arr = current($arr);
  396. else
  397. $arr = null;
  398. return $arr;
  399. }
  400. public static function rmVal($array, $value){
  401. if(self::isAvailable($array) && $value){
  402. foreach($array as $key => $val){
  403. if($val === $value){
  404. unset($array[$key]);
  405. }
  406. }
  407. self::tidy($array);
  408. }
  409. return $array;
  410. }
  411. public static function clear(&$array){
  412. if(self::isAvailable($array)){
  413. foreach($array as $key => $val){
  414. if(is_string($val)){
  415. $array[$key] = trim($val);
  416. if(!$array[$key]){
  417. unset($array[$key]);
  418. continue;
  419. }
  420. }
  421. else if(!$val){
  422. unset($array[$key]);
  423. continue;
  424. }
  425. }
  426. self::tidy($array);
  427. return true;
  428. }
  429. return false;
  430. }
  431. public static function isSeted($array, $keys){
  432. if(self::isAvailable($array) && (is_string($keys) || self::isAvailable($keys))){
  433. if(is_string($keys))
  434. $keys = array($keys);
  435. $keysCount = count($keys);
  436. $setedKeysCount = 0;
  437. foreach($array as $key => $val){
  438. foreach($keys as $k){
  439. if($key === $k && ((is_string($val) && trim($val) != null) || $val)){
  440. $setedKeysCount++;
  441. }
  442. }
  443. }
  444. if($setedKeysCount == $keysCount){
  445. return true;
  446. }
  447. }
  448. return false;
  449. }
  450. public static function getVals($array, $mappings = array()){
  451. $vals = array();
  452. if(self::isAvailable($array) && self::isAvailable($mappings)){
  453. foreach($mappings as $key => $val){
  454. if(isset($array[$val])){
  455. if(!is_int($key))
  456. $vals[$key] = $array[$val];
  457. else
  458. $vals[$val] = $array[$val];
  459. }
  460. unset($array[$val]);
  461. }
  462. }
  463. return $vals;
  464. }
  465. }