helper.php 3.9 KB

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