forked from hissy/rs-csv-importer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-rs_csv_helper.php
44 lines (35 loc) · 1000 Bytes
/
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
<?php
/**
* A helper class for get data from CSV files.
*
* @package Really Simple CSV Importer
*/
class RS_CSV_Helper {
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;
}
}