functions.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. use Hashids\Hashids;
  3. /**
  4. * Here is your custom functions.
  5. */
  6. /*
  7. 快速排序
  8. */
  9. function quickSort($array)
  10. {
  11. if(!isset($array[1]))
  12. return $array;
  13. $mid = $array[0]; //获取一个用于分割的关键字,一般是首个元素
  14. $leftArray = array();
  15. $rightArray = array();
  16. foreach($array as $v)
  17. {
  18. if($v > $mid)
  19. $rightArray[] = $v; //把比$mid大的数放到一个数组里
  20. if($v < $mid)
  21. $leftArray[] = $v; //把比$mid小的数放到另一个数组里
  22. }
  23. $leftArray = quickSort($leftArray); //把比较小的数组再一次进行分割
  24. $leftArray[] = $mid; //把分割的元素加到小的数组后面,不能忘了它哦
  25. $rightArray = quickSort($rightArray); //把比较大的数组再一次进行分割
  26. return array_merge($leftArray,$rightArray); //组合两个结果
  27. }
  28. // 获取Hashids 对象
  29. function getHashids(){
  30. global $hashids;
  31. if($hashids instanceof Hashids ){
  32. return $hashids;
  33. }
  34. $hashids = new Hashids('D6M97LIvpp4qWuz3nKzqi6yYN4GAA61b',32);
  35. return $hashids;
  36. }
  37. /**
  38. * 获取对象或数组的属性值
  39. * @param $param
  40. * @param $key
  41. * @param string $default
  42. * @return mixed|string
  43. */
  44. function getProp($param, $key, $default = '')
  45. {
  46. $result = $default;
  47. if (is_object($param) && isset($param->$key)) {
  48. $result = $param->$key;
  49. }
  50. if (is_array($param) && isset($param[$key])) {
  51. $result = $param[$key];
  52. }
  53. return $result;
  54. }