helpers_array.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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('import_xls')) {
  69. function import_xls($filepath, $fields=[], $encode_utf8=false) {
  70. $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filepath);
  71. $sheetData = $spreadsheet->getActiveSheet()->toArray();
  72. //pre($sheetData, 1);
  73. $headers = [];
  74. if (! empty($fields)) {
  75. $headers = $fields;
  76. }
  77. $rows = [];
  78. $line_idx = 0;
  79. foreach ($sheetData as $input_row) {
  80. $line_idx++;
  81. if ($line_idx <= 1) {
  82. // ligne d'entete
  83. if (empty($headers)) {
  84. foreach ($input_row as $key => $value) {
  85. if ($encode_utf8) {
  86. $headers[$key] = utf8_encode($value);
  87. } else {
  88. $headers[$key] = $value;
  89. }
  90. }
  91. }
  92. continue;
  93. }else{
  94. // lignes de data
  95. $row = [];
  96. $row_ok = true;
  97. $col_idx = 0;
  98. foreach ($input_row as $key => $value) {
  99. $col_idx++;
  100. if ($col_idx > count($headers)) {
  101. continue;
  102. }
  103. if (! isset($headers[$key])) {
  104. $row_ok = false;
  105. //pre($input_row);
  106. break;
  107. }
  108. $header_key = $headers[$key];
  109. if ($encode_utf8) {
  110. $row[$header_key] = utf8_encode($value);
  111. } else {
  112. $row[$header_key] = $value;
  113. }
  114. }
  115. if ($row_ok) {
  116. $rows[] = $row;
  117. }
  118. }
  119. }
  120. return $rows;
  121. }
  122. }
  123. if (! function_exists('import_csv')) {
  124. function import_csv($filepath, $separator=";", $fields=[], $encode_utf8=true) {
  125. $rows = [];
  126. $handle = fopen($filepath, "r");
  127. $headers = [];
  128. if (! empty($fields)) {
  129. $headers = $fields;
  130. }
  131. $line_idx = 0;
  132. while (($input_row = fgetcsv($handle, 4096, $separator)) !== false) {
  133. $line_idx++;
  134. if ($line_idx <= 1) {
  135. // ligne d'entete
  136. if (empty($headers)) {
  137. foreach ($input_row as $key => $value) {
  138. if ($encode_utf8) {
  139. $headers[$key] = utf8_encode($value);
  140. } else {
  141. $headers[$key] = $value;
  142. }
  143. }
  144. }
  145. continue;
  146. }else{
  147. // lignes de data
  148. $row = [];
  149. foreach ($input_row as $key => $value) {
  150. $header_key = $headers[$key];
  151. if ($encode_utf8) {
  152. $row[$header_key] = utf8_encode($value);
  153. } else {
  154. $row[$header_key] = $value;
  155. }
  156. }
  157. $rows[] = $row;
  158. }
  159. }
  160. fclose($handle);
  161. return $rows;
  162. }
  163. }
  164. if (! function_exists('get_csv')) {
  165. function get_csv($arr, $fields=array(), $sep=";") {
  166. $str = '';
  167. if (! empty($arr)) {
  168. if (empty($fields)) {
  169. $fields = array_keys($arr[0]);
  170. }
  171. $line = array();
  172. foreach ($fields as $k => $v) {
  173. if (! is_numeric($k)) {
  174. $line[] = $k;
  175. } else {
  176. $line[] = $v;
  177. }
  178. }
  179. $str .= implode($sep, $line) . PHP_EOL;
  180. foreach ($arr as $row) {
  181. $line = array();
  182. foreach ($fields as $field) {
  183. $val = $row[$field];
  184. if (is_numeric($val) && substr($val."", 0, 1) === "0" && strlen($val."") > 1) {
  185. // pour exporter correctement dans Excel les numeros de telephone commencant par 0
  186. $val = '="' . $val . '"';
  187. }
  188. $line[] = $val;
  189. }
  190. //$str .= implode($sep, $line) . PHP_EOL;
  191. //$str .= '"' . implode('"' . $sep . '"', str_replace('"', '\\"', $line)) . '"' . PHP_EOL;
  192. $str .= '"' . implode('"' . $sep . '"', str_replace('"', '""', $line)) . '"' . PHP_EOL;
  193. }
  194. }
  195. return $str;
  196. }
  197. }
  198. if (! function_exists('exportToCsvFile')) {
  199. function exportToCsvFile($rows, $export_filename=null, $fields=null) {
  200. $csv_content = get_csv($rows, $fields);
  201. if (! empty($export_filename)) {
  202. // download file
  203. header('Content-Type: text/csv');
  204. header('Content-Disposition: attachment;filename=' . basename($export_filename));
  205. header("Pragma: no-cache");
  206. header("Expires: 0");
  207. } else {
  208. // show in browser
  209. header('Content-Type: text/plain');
  210. }
  211. echo $csv_content;
  212. exit;
  213. }
  214. }
  215. if (! function_exists('array_map_with_keys')) {
  216. function array_map_with_keys($func, $array) {
  217. $ret = [];
  218. foreach ($array as $k => $v) {
  219. $ret[$k] = $func($v, $k);
  220. }
  221. return $ret;
  222. }
  223. }