-
Notifications
You must be signed in to change notification settings - Fork 83
/
class-rs_csv_helper.php
59 lines (46 loc) · 1.27 KB
/
class-rs_csv_helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* A helper class for get data from CSV files.
*
* @package Really Simple CSV Importer
*/
class RS_CSV_Helper {
const DELIMITER = ",";
// File utility functions
public function fopen($filename, $mode='r') {
return fopen($filename, $mode);
}
public function fgetcsv($handle, $length = 0) {
return fgetcsv($handle, $length, self::DELIMITER);
}
public function fclose($fp) {
return fclose($fp);
}
public function parse_columns(&$obj, $array) {
if (!is_array($array) || count($array) == 0)
return false;
$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 == strncmp($array[0], $bom, 3)) {
$array[0] = substr($array[0], 3);
}
$keys = array_keys($array);
$values = array_values($array);
$obj->column_indexes = array_combine($values, $keys);
$obj->column_keys = array_combine($keys, $values);
}
public function get_data($obj, &$array, $key) {
if (!isset($obj->column_indexes) || !is_array($array) || count($array) == 0)
return false;
if (isset($obj->column_indexes[$key])) {
$index = $obj->column_indexes[$key];
if (isset($array[$index]) && !empty($array[$index])) {
$value = $array[$index];
unset($array[$index]);
return $value;
} elseif (isset($array[$index])) {
unset($array[$index]);
}
}
return false;
}
}