-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharray-to-csv.php
43 lines (36 loc) · 1.12 KB
/
array-to-csv.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
<?php
/**
* Convert a PHP array into CSV
*
* @author Jon Segador <[email protected]> || http://jonsegador.com
* https://github.com/jonseg/array-to-csv
*
*/
class arrayToCsv{
protected $delimiter;
protected $text_separator;
protected $replace_text_separator;
protected $line_delimiter;
public function __construct($delimiter = ";", $text_separator = '"', $replace_text_separator = "'", $line_delimiter = "\n"){
$this->delimiter = $delimiter;
$this->text_separator = $text_separator;
$this->replace_text_separator = $replace_text_separator;
$this->line_delimiter = $line_delimiter;
}
public function convert($input) {
$lines = array();
foreach ($input as $v) {
$lines[] = $this->convertLine($v);
}
return implode($this->line_delimiter, $lines);
}
private function convertLine($line) {
$csv_line = array();
foreach ($line as $v) {
$csv_line[] = is_array($v) ?
$this->convertLine($v) :
$this->text_separator . str_replace($this->text_separator, $this->replace_text_separator, $v) . $this->text_separator;
}
return implode($this->delimiter, $csv_line);
}
}