helpers_array.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, $preserve_keys=true) {
  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. if ($preserve_keys) {
  50. $results[$key_value][$k] = $v;
  51. } else {
  52. $results[$key_value][] = $v;
  53. }
  54. }
  55. return $results;
  56. }
  57. }
  58. if (! function_exists('arrayToList')) {
  59. function arrayToList($array) {
  60. $results = array();
  61. $db = App::getDb();
  62. foreach ($array as $k => $v) {
  63. $results[] = $db->escape($v);
  64. }
  65. return implode(', ', $results);
  66. }
  67. }
  68. if (! function_exists('get_csv')) {
  69. function get_csv($arr, $fields=array(), $sep=";") {
  70. $str = '';
  71. if (! empty($arr)) {
  72. if (empty($fields)) {
  73. $fields = array_keys($arr[0]);
  74. }
  75. $line = array();
  76. foreach ($fields as $k => $v) {
  77. if (! is_numeric($k)) {
  78. $line[] = $k;
  79. } else {
  80. $line[] = $v;
  81. }
  82. }
  83. $str .= implode($sep, $line) . PHP_EOL;
  84. foreach ($arr as $row) {
  85. $line = array();
  86. foreach ($fields as $field) {
  87. $val = $row[$field];
  88. if (is_numeric($val) && substr($val."", 0, 1) === "0" && strlen($val."") > 1) {
  89. // pour exporter correctement dans Excel les numeros de telephone commencant par 0
  90. $val = '="' . $val . '"';
  91. }
  92. $line[] = $val;
  93. }
  94. //$str .= implode($sep, $line) . PHP_EOL;
  95. //$str .= '"' . implode('"' . $sep . '"', str_replace('"', '\\"', $line)) . '"' . PHP_EOL;
  96. $str .= '"' . implode('"' . $sep . '"', str_replace('"', '""', $line)) . '"' . PHP_EOL;
  97. }
  98. }
  99. return $str;
  100. }
  101. }
  102. if (! function_exists('exportToCsvFile')) {
  103. function exportToCsvFile($rows, $export_filename=null, $fields=null) {
  104. if (! empty($export_filename)) {
  105. // download file
  106. header('Content-Type: text/csv');
  107. header('Content-Disposition: attachment;filename=' . basename($export_filename));
  108. header("Pragma: no-cache");
  109. header("Expires: 0");
  110. } else {
  111. // show in browser
  112. header('Content-Type: text/plain');
  113. }
  114. echo get_csv($rows, $fields);
  115. exit;
  116. }
  117. }
  118. if (! function_exists('array_map_with_keys')) {
  119. function array_map_with_keys($func, $array) {
  120. $ret = [];
  121. foreach ($array as $k => $v) {
  122. $ret[$k] = $func($v, $k);
  123. }
  124. return $ret;
  125. }
  126. }