CFunc.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fec\helpers;
  10. use Yii;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class CFunc
  16. {
  17. # 1.object 转换成 数组。
  18. public static function object_to_array($obj)
  19. {
  20. //$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  21. if(is_object($obj) || is_array($obj)){
  22. if(is_object($obj)){
  23. $_arr = get_object_vars($obj);
  24. }else{
  25. $_arr = $obj;
  26. }
  27. foreach ($_arr as $key => $val)
  28. {
  29. $val = (is_array($val) || is_object($val)) ? self::object_to_array($val) : $val;
  30. $arr[$key] = $val;
  31. }
  32. return $arr;
  33. }else{
  34. return $obj;
  35. }
  36. }
  37. # 2.是否是时间格式
  38. public static function is_time($time)
  39. {
  40. $time = trim($time);
  41. $pattern1 = '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/s';
  42. $r1 = preg_match($pattern1, $time);
  43. $pattern2 = '/^\d{4}-\d{2}-\d{2}$/s';
  44. $r2 = preg_match($pattern2, $time);
  45. return ($r1 || $r2 );
  46. }
  47. # 3.对二维数组进行排序
  48. /* 譬如数组
  49. $arr = [
  50. ['name' = 'x1','age'=33],
  51. ['name' = 'y1','age'=11],
  52. ['name' = 'a1','age'=66],
  53. ['name' = 't1','age'=44],
  54. ];
  55. $result = CFunc::array_sort($arr,'name','asc');
  56. */
  57. # 二维数组进行排序
  58. # $array为要排序的数组
  59. # $keys为要用来排序的键名,二维数组的key
  60. # $type默认为升序排序
  61. public static function array_sort($array,$keys,$dir='asc',$isFloat=true){
  62. $keysvalue = $new_array = array();
  63. foreach ($array as $k=>$v){
  64. if($isFloat){
  65. $keysvalue[$k] = (float)$v[$keys];
  66. }else{
  67. $keysvalue[$k] = $v[$keys];
  68. }
  69. }
  70. if($dir == 'asc'){
  71. asort($keysvalue);
  72. }else{
  73. arsort($keysvalue);
  74. }
  75. reset($keysvalue);
  76. foreach ($keysvalue as $k=>$v){
  77. $new_array[$k] = $array[$k];
  78. }
  79. return $new_array;
  80. }
  81. # 4.得到真实的IP
  82. public static function get_real_ip(){
  83. $ip=false;
  84. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  85. $ip = $_SERVER["HTTP_CLIENT_IP"];
  86. }
  87. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  88. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  89. if ($ip) {
  90. array_unshift($ips, $ip); $ip = FALSE;
  91. }
  92. for ($i = 0; $i < count($ips); $i++) {
  93. if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) {
  94. $ip = $ips[$i];
  95. break;
  96. }
  97. }
  98. }
  99. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  100. }
  101. # 得到4位小数点
  102. public function getFormatFloat($data,$decimal=4){
  103. return round($data,$decimal);
  104. }
  105. }