123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- <?php
- /**
- * The Feeler framework, licensed under Mit license
- * Author: Rick Guo
- */
- class Arr{
- //Array of keys to delete and restore to incrementing key
- public static function tidy(&$array = array()){
- if($array){
- array_unshift($array, array());
- array_splice($array, 0, 1);
-
- return true;
- }
-
- return false;
- }
-
- //Sort the 2D array by the 2nd dimension value, according is the 2nd dimension key
- public static function sortByField(&$array, $field, $order = "ASC", $type = "SORT_NATURAL", $keepKey = false){
- if(!is_array($array) || !$array || !is_string($field) || trim($field) == "")
- return false;
-
- $arr1 = $arr2 = array();
-
- foreach($array as $key => $val){
- if(isset($val[$field])) $arr1[$key] = $val[$field];
- }
-
- if($type == "SORT_NATURAL"){
- if($order == "ASC"){
- natsort($arr1);
- }
- else if($order == "DESC"){
- natsort($arr1);
- $arr1 = array_reverse($arr1, true);
- }
- else
- return false;
- }
- else{
- switch($type){
- case "SORT_NUMERIC":
- $type = SORT_NUMERIC;
- break;
-
- case "SORT_REGULAR":
- $type = SORT_REGULAR;
- break;
-
- case "SORT_STRING":
- $type = SORT_STRING;
- break;
-
- case "SORT_LOCALE_STRING":
- $type = SORT_LOCALE_STRING;
- break;
-
- default:
- return false;
- break;
- }
-
- if($order === "ASC")
- asort($arr1, $type);
- else if($order === "DESC")
- arsort($arr1, $type);
- else
- return false;
- }
-
- if($keepKey){
- foreach($arr1 as $key => $val){
- $arr2[$key] = $array[$key];
- }
- }
- else{
- foreach($arr1 as $key => $val){
- $arr2[] = $array[$key];
- }
- }
- $array = $arr2;
- return true;
- }
-
- public static function sort(&$array, $order = "ASC", $type = "SORT_NATURAL", $keepKey = true){
- if(!is_array($array) || !$array)
- return false;
-
- if($type == "SORT_NATURAL"){
- if($order == "ASC"){
- natsort($array);
- }
- else if($order == "DESC"){
- natsort($array);
- $array = array_reverse($array, true);
- }
- else
- return false;
- }
- else{
- switch($type){
- case "SORT_NUMERIC":
- $type = SORT_NUMERIC;
- break;
-
- case "SORT_REGULAR":
- $type = SORT_REGULAR;
- break;
-
- case "SORT_STRING":
- $type = SORT_STRING;
- break;
-
- case "SORT_LOCALE_STRING":
- $type = SORT_LOCALE_STRING;
- break;
-
- default:
- return false;
- break;
- }
-
- if($order === "ASC")
- asort($array, $type);
- else if($order === "DESC")
- arsort($array, $type);
- else
- return false;
- }
-
- if(!$keepKey)
- self::tidy($array);
-
- return true;
- }
-
- public static function ksort(&$array, $order = "ASC", $type = "SORT_NATURAL"){
- if(!is_array($array) || !$array)
- return false;
-
- if($type == "SORT_NATURAL"){
- $keys = array_keys($array);
-
- if($order == "ASC"){
- natsort($keys);
- }
- else if($order == "DESC"){
- natsort($keys);
- $keys = array_reverse($keys, true);
- }
- else
- return false;
-
- $arr1 = array();
- foreach($keys as $key){
- foreach($array as $k => $v){
- if($key === $k){
- $arr1[$key] = $v;
- }
- }
- }
- $array = $arr1;
-
- return true;
- }
- else{
- switch($type){
- case "SORT_NUMERIC":
- $type = SORT_NUMERIC;
- break;
-
- case "SORT_REGULAR":
- $type = SORT_REGULAR;
- break;
-
- case "SORT_STRING":
- $type = SORT_STRING;
- break;
-
- case "SORT_LOCALE_STRING":
- $type = SORT_LOCALE_STRING;
- break;
-
- default:
- return false;
- break;
- }
-
- if($order === "ASC")
- ksort($array, $type);
- else if($order === "DESC")
- krsort($array, $type);
- else
- return false;
-
- return true;
- }
-
- return false;
- }
-
- public static function slice($array, $offset, $length = null, $keepKey = true){
- return array_slice($array, $offset, $length, $keepKey);
- }
-
- public static function makeOrderField(&$array, $field, $start = 1){
- if(!self::isAvailable($array))
- return false;
- if(!$field)
- return false;
- if(!is_int($start))
- return false;
-
- foreach($array as $key => $val){
- $array[$key][$field] = $start;
-
- $start++;
- }
-
- return true;
- }
-
- //merge multi array and make the values different
- public static function merge($array){
- $params = func_get_args();
- if(isset($params[1])){
- self::tidy($array);
- unset($params[0]);
- foreach($params as $param){
- if(is_array($param)){
- foreach($param as $val){
- if(is_string($val))
- $val = trim($val);
-
- if(!in_array($val, $array))
- $array[] = $val;
- }
- }
- }
- }
- return $array;
- }
-
- //merge multi array and make the values different
- public static function plus($array){
- $params = func_get_args();
-
- if(isset($params[1])){
- self::tidy($array);
- unset($params[0]);
- foreach($params as $param){
- if(is_array($param)){
- foreach($param as $key => $val){
- if(is_string($val))
- $val = trim($val);
-
- if(!array_key_exists($key, $array))
- $array[$key] = $val;
- }
- }
- }
- }
-
- return $array;
- }
-
- //merge multi array and make the values different
- public static function pack($array){
- $params = func_get_args();
-
- if(isset($params[1])){
- unset($params[0]);
- foreach($params as $param){
- if(is_array($param)){
- foreach($param as $key => $val){
- if(is_string($val))
- $val = trim($val);
-
- if(!empty($key) || $key === 0)
- $array[$key] = $val;
- }
- }
- }
- }
-
- return $array;
- }
-
- //merge multi array and don't check the values are unique or not
- public static function mergeAll($array){
- $params = func_get_args();
-
- if(isset($params[1])){
- self::tidy($array);
- unset($params[0]);
- foreach($params as $param){
- foreach($param as $val){
- if(is_string($val))
- $val = trim($val);
-
- $array[] = $val;
- }
- }
- }
-
- return $array;
- }
-
- public static function unique($array){
- if(is_array($array) && $array){
- $newArray = array();
-
- foreach($array as $key => $val){
- if(is_string($val))
- $val = trim($val);
-
- if(!in_array($val, $newArray))
- $newArray[$key] = $val;
- }
-
- return $newArray;
- }
-
- return $array;
- }
-
- //For large data analysis of 2D data merge stack numbers other elements only
- public static function mergeByKey($array){
- $params = func_get_args();
-
- if(isset($params[1])){
- unset($params[0]);
- if(!$array)
- $array = array();
-
- foreach($params as $param){
- foreach($param as $key => $arr){
- if(!$arr || !is_array($arr))
- continue;
- if(!isset($array[$key]))
- $array[$key] = $arr;
- else{
- foreach($arr as $k => $v){
- if(isset($array[$key][$k])){
- if(is_numeric($array[$key][$k]) && is_numeric($v)){
- $array[$key][$k] += $v;
- }
- else if(is_array($array[$key][$k]) && is_array($v)){
- $array[$key][$k] = self::merge($array[$key][$k], $v);
- }
- }
- }
- }
- }
- }
- }
-
- return $array;
- }
-
- //count the deepest dimension of the array
- public static function countDimension($array){
- $dimension = array();
-
- if(is_array($array)){
- foreach($array as $key => $arr){
- if(!isset($dimension[$key]))
- $dimension[$key] = 0;
-
- if(is_array($arr)){
- $dimension[$key] += self::countDimension($arr);
- }
- }
-
- arsort($dimension, SORT_NUMERIC);
- $dimension = current($dimension) + 1;
- }
- else
- $dimension = 1;
-
- return $dimension;
- }
-
- //array to object conversion
- public static function toObj($arr){
- if(gettype($arr) != "array") return null;
- foreach($arr as $k => $v){
- if(gettype($v) == "array" || getType($v) == "object")
- $arr[$k] = (object)self::toObj($v);
- }
- return (object)$arr;
- }
-
- //checkout the values in first array, but not in the other arrays
- public static function diff($array){
- if(!is_array($array))
- return $array;
-
- $params = func_get_args();
- $diff = array();
-
- if(isset($params[1])){
- unset($params[0]);
-
- foreach($params as $arr){
- if(!is_array($arr))
- continue;
-
- foreach($array as $key => $val){
- if(!in_array($val, $arr) && !in_array($val, $diff))
- $diff[] = $val;
- }
- }
- }
-
- return $diff;
- }
-
- //checkout the same values between the first array and the other arrays
- public static function intersect($array){
- if(!is_array($array))
- return $array;
-
- $params = func_get_args();
- $same = array();
-
- if(isset($params[1])){
- unset($params[0]);
-
- foreach($params as $arr){
- if(!is_array($arr))
- continue;
-
- foreach($arr as $key => $val){
- if(in_array($val, $array) && !in_array($val, $same))
- $same[] = $val;
- }
- }
- }
-
- return $same;
- }
-
- public static function isAvailable($arr, $key = null){
- if(!is_array($arr))
- return false;
-
- if(!$key)
- return $arr ? true : false;
-
- if(is_array($key)){
- foreach($key as $k){
- if($matches = Regex::matchKeyWithType($k)){
- if(!Type::verify($matches[1], $matches[0]))
- return false;
- }
-
- if(!isset($arr[$k]))
- return false;
- }
- }
- else{
- if($matches = Regex::matchKeyWithType($key)){
- if(!Type::verify($matches[1], $matches[0]))
- return false;
- }
-
- if(!isset($arr[$key]))
- return false;
- }
-
- return true;
- }
-
- public static function keyExists($arr, $key){
- return isset($arr[$key]) ? true : false;
- }
-
- public static function current($arr){
- if(self::isAvailable($arr))
- $arr = current($arr);
- else
- $arr = null;
-
- return $arr;
- }
-
- public static function rmVal($array, $value){
- if(self::isAvailable($array) && $value){
- foreach($array as $key => $val){
- if($val === $value){
- unset($array[$key]);
- }
- }
-
- self::tidy($array);
- }
-
- return $array;
- }
-
- public static function clear(&$array){
- if(self::isAvailable($array)){
- foreach($array as $key => $val){
- if(is_string($val)){
- $array[$key] = trim($val);
- if(!$array[$key]){
- unset($array[$key]);
- continue;
- }
- }
- else if(!$val){
- unset($array[$key]);
- continue;
- }
- }
-
- self::tidy($array);
-
- return true;
- }
-
- return false;
- }
-
- public static function isSeted($array, $keys){
- if(self::isAvailable($array) && (is_string($keys) || self::isAvailable($keys))){
- if(is_string($keys))
- $keys = array($keys);
-
- $keysCount = count($keys);
- $setedKeysCount = 0;
-
- foreach($array as $key => $val){
- foreach($keys as $k){
- if($key === $k && ((is_string($val) && trim($val) != null) || $val)){
- $setedKeysCount++;
- }
- }
- }
-
- if($setedKeysCount == $keysCount){
- return true;
- }
- }
-
- return false;
- }
-
- public static function getVals($array, $mappings = array()){
- $vals = array();
-
- if(self::isAvailable($array) && self::isAvailable($mappings)){
- foreach($mappings as $key => $val){
- if(isset($array[$val])){
- if(!is_int($key))
- $vals[$key] = $array[$val];
- else
- $vals[$val] = $array[$val];
- }
-
- unset($array[$val]);
- }
- }
-
- return $vals;
- }
- }
|