helper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\library;
  10. /**
  11. * 工具类
  12. * Class helper
  13. * @package app\common\library
  14. */
  15. class helper
  16. {
  17. /**
  18. * 获取数组中指定的列
  19. * @param $source
  20. * @param $column
  21. * @return array
  22. */
  23. public static function getArrayColumn($source, $column)
  24. {
  25. $columnArr = [];
  26. foreach ($source as $item) {
  27. $columnArr[] = $item[$column];
  28. }
  29. return $columnArr;
  30. }
  31. /**
  32. * 获取数组中指定的列
  33. * @param $source
  34. * @param $columns
  35. * @return array
  36. */
  37. public static function getArrayColumns($source, $columns)
  38. {
  39. $columnArr = [];
  40. foreach ($source as $item) {
  41. $temp = [];
  42. foreach ($columns as $index) {
  43. $temp[$index] = $item[$index];
  44. }
  45. $columnArr[] = $temp;
  46. }
  47. return $columnArr;
  48. }
  49. /**
  50. * 把二维数组中某列设置为key返回
  51. * @param $source
  52. * @param $index
  53. * @return array
  54. */
  55. public static function arrayColumn2Key($source, $index)
  56. {
  57. $data = [];
  58. foreach ($source as $item) {
  59. $data[$item[$index]] = $item;
  60. }
  61. return $data;
  62. }
  63. public static function number2($number, $isMinimum = false, $minimum = 0.01)
  64. {
  65. $isMinimum && $number = max($minimum, $number);
  66. return sprintf('%.2f', $number);
  67. }
  68. public static function getArrayItemByColumn($array, $column, $value)
  69. {
  70. foreach ($array as $item) {
  71. if ($item[$column] == $value) {
  72. return $item;
  73. }
  74. }
  75. return false;
  76. }
  77. public static function getArrayColumnSum($array, $column)
  78. {
  79. $sum = 0;
  80. foreach ($array as $item) {
  81. $sum += $item[$column] * 100;
  82. }
  83. return $sum / 100;
  84. }
  85. public static function setDataAttribute(&$source, $defaultData, $isArray = false)
  86. {
  87. if (!$isArray) $dataSource = [&$source]; else $dataSource = &$source;
  88. foreach ($dataSource as &$item) {
  89. foreach ($defaultData as $key => $value) {
  90. $item[$key] = $value;
  91. }
  92. }
  93. return $source;
  94. }
  95. public static function bcsub($leftOperand, $rightOperand, $scale = 2)
  96. {
  97. return \bcsub($leftOperand, $rightOperand, $scale);
  98. }
  99. public static function bcadd($leftOperand, $rightOperand, $scale = 2)
  100. {
  101. return \bcadd($leftOperand, $rightOperand, $scale);
  102. }
  103. public static function bcmul($leftOperand, $rightOperand, $scale = 2)
  104. {
  105. return \bcmul($leftOperand, $rightOperand, $scale);
  106. }
  107. public static function bcdiv($leftOperand, $rightOperand, $scale = 2)
  108. {
  109. return \bcdiv($leftOperand, $rightOperand, $scale);
  110. }
  111. public static function bccomp($leftOperand, $rightOperand, $scale = 2)
  112. {
  113. return \bccomp($leftOperand, $rightOperand, $scale);
  114. }
  115. public static function bcequal($leftOperand, $rightOperand, $scale = 2)
  116. {
  117. return self::bccomp($leftOperand, $rightOperand, $scale) === 0;
  118. }
  119. /**
  120. * 数组转义为json
  121. * @param array|\think\Collection $data
  122. * @return string
  123. */
  124. public static function jsonEncode($data)
  125. {
  126. return json_encode($data, JSON_UNESCAPED_UNICODE);
  127. }
  128. /**
  129. * json转义为数组
  130. * @param $json
  131. * @return array
  132. */
  133. public static function jsonDecode($json)
  134. {
  135. return json_decode($json, true);
  136. }
  137. }