helpers_array.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. use \KarmaFW\App;
  3. if (! function_exists('arrayReduceToOneColumn')) {
  4. function arrayReduceToOneColumn($array, $column_key) {
  5. return array_map(function($row) use ($column_key) {
  6. if (is_callable($column_key)) {
  7. return $column_key($row);
  8. } else {
  9. return $row[$column_key];
  10. }
  11. }, $array);
  12. }
  13. }
  14. if (! function_exists('arrayAddKeyFromColumn')) {
  15. function arrayAddKeyFromColumn($array, $column_key) {
  16. $results = array();
  17. foreach ($array as $row) {
  18. if (is_callable($column_key)) {
  19. $key = $column_key($row);
  20. } else if (is_array($column_key)) {
  21. $key_parts = [];
  22. foreach ($column_key as $column_key_item) {
  23. $key_parts[] = $row[$column_key_item];
  24. }
  25. $key = implode('-', $key_parts);
  26. }else{
  27. $key = $row[$column_key];
  28. }
  29. $results[$key] = $row;
  30. }
  31. if (empty($results)) {
  32. //return new stdClass();
  33. }
  34. return $results;
  35. }
  36. }
  37. if (! function_exists('arrayGroupByColumn')) {
  38. function arrayGroupByColumn($array, $column_key) {
  39. $results = array();
  40. foreach ($array as $k => $v) {
  41. if (is_callable($column_key)) {
  42. $key_value = $column_key($v);
  43. } else {
  44. $key_value = $v[$column_key];
  45. }
  46. if (! isset($results[$key_value])) {
  47. $results[$key_value] = array();
  48. }
  49. $results[$key_value][$k] = $v;
  50. }
  51. return $results;
  52. }
  53. }
  54. if (! function_exists('arrayToList')) {
  55. function arrayToList($array) {
  56. $results = array();
  57. $db = App::getDb();
  58. foreach ($array as $k => $v) {
  59. $results[] = $db->escape($v);
  60. }
  61. return implode(', ', $results);
  62. }
  63. }
  64. if (! function_exists('get_csv')) {
  65. function get_csv($arr, $fields=array(), $sep=";") {
  66. $str = '';
  67. if (! empty($arr)) {
  68. if (empty($fields)) {
  69. $fields = array_keys($arr[0]);
  70. }
  71. $line = array();
  72. foreach ($fields as $k => $v) {
  73. if (! is_numeric($k)) {
  74. $line[] = $k;
  75. } else {
  76. $line[] = $v;
  77. }
  78. }
  79. $str .= implode($sep, $line) . PHP_EOL;
  80. foreach ($arr as $row) {
  81. $line = array();
  82. foreach ($fields as $field) {
  83. $line[] = $row[$field];
  84. }
  85. //$str .= implode($sep, $line) . PHP_EOL;
  86. //$str .= '"' . implode('"' . $sep . '"', str_replace('"', '\\"', $line)) . '"' . PHP_EOL;
  87. $str .= '"' . implode('"' . $sep . '"', str_replace('"', '""', $line)) . '"' . PHP_EOL;
  88. }
  89. }
  90. return $str;
  91. }
  92. }
  93. if (! function_exists('exportToCsvFile')) {
  94. function exportToCsvFile($rows, $export_filename=null, $fields=null) {
  95. if (! empty($export_filename)) {
  96. // download file
  97. header('Content-Type: text/csv');
  98. header('Content-Disposition: attachment;filename=' . basename($export_filename));
  99. header("Pragma: no-cache");
  100. header("Expires: 0");
  101. } else {
  102. // show in browser
  103. header('Content-Type: text/plain');
  104. }
  105. echo get_csv($rows, $fields);
  106. exit;
  107. }
  108. }