123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- class Import {
-
- function csv_to_array($filename) {
- $result = array();
-
- $handle = fopen($filename, 'r');
- if ($handle === false) {
- return $result;
- }
- $buffer = array();
- $i = 0;
- while (!feof($handle) && $i < 200) {
-
- $buffer[] = fgets($handle);
- $i++;
- }
- fclose($handle);
- $buffer = implode("\n", $buffer);
- $from_encoding = api_detect_encoding($buffer);
- unset($buffer);
-
- $handle = fopen($filename, 'r');
- if ($handle === false) {
- return $result;
- }
- $keys = api_fgetcsv($handle, null, ';');
- foreach ($keys as $key => &$key_value) {
- $key_value = api_to_system_encoding($key_value, $from_encoding);
- }
- while (($row_tmp = api_fgetcsv($handle, null, ';')) !== false) {
- $row = array();
-
- if (is_array($row_tmp) && count($row_tmp) > 0 && $row_tmp[0] != '') {
- if (!is_null($row_tmp[0])) {
- foreach ($row_tmp as $index => $value) {
- $row[$keys[$index]] = api_to_system_encoding($value, $from_encoding);
- }
- $result[] = $row;
- }
- }
- }
- fclose($handle);
- return $result;
- }
- }
|