$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; } }