public function toArray() { $temp = $this->csv->jsonSerialize(); $headings = $temp[0]; $result = $headings; if (count($temp) > 1) { $result = []; for ($i = 1; $i < count($temp); ++$i) { $row = []; for ($j = 0; $j < count($headings); ++$j) { $row[$headings[$j]] = $temp[$i][$j]; } $expanded = []; foreach ($row as $key => $value) { ArrayHelpers::set($expanded, $key, $value); } $result[] = $expanded; } } return $result; }
/** * Ported from laravel-formatter * https://github.com/SoapBox/laravel-formatter * * @author Daniel Berry <*****@*****.**> * @license MIT License (see LICENSE.readme included in the bundle) * * Return a csv representation of the data stored in the parser * * @return string An csv string representing the encapsulated data */ public function toCsv($newline = "\n", $delimiter = ",", $enclosure = '"', $escape = "\\") { $data = $this->toArray(); if (ArrayHelpers::isAssociative($data) || !is_array($data[0])) { $data = [$data]; } $escaper = function ($items) use($enclosure, $escape) { return array_map(function ($item) use($enclosure, $escape) { return str_replace($enclosure, $escape . $enclosure, $item); }, $items); }; $headings = ArrayHelpers::dotKeys($data[0]); $result = []; foreach ($data as $row) { $result[] = array_values(ArrayHelpers::dot($row)); } $data = $result; $output = $enclosure . implode($enclosure . $delimiter . $enclosure, $escaper($headings)) . $enclosure . $newline; foreach ($data as $row) { $output .= $enclosure . implode($enclosure . $delimiter . $enclosure, $escaper((array) $row)) . $enclosure . $newline; } return rtrim($output, $newline); }
/** * Return a csv representation of the data stored in the parser * * @return string An csv string representing the encapsulated data */ public function toCsv() { $data = $this->toArray(); if (ArrayHelpers::isAssociative($data) || !is_array($data[0])) { $data = [$data]; } $result = []; $result[] = ArrayHelpers::dotKeys($data[0]); foreach ($data as $row) { $result[] = array_values(ArrayHelpers::dot($row)); } $output = ''; $count = 0; foreach ($result as $row) { if ($count != 0) { $output .= "\r\n"; } $count++; $output .= implode(',', $row); } return $output; }