method toCsv documentation in bbn\X

Formats an array as a CSV string.

Adapted from http://us3.php.net/manual/en/function.fputcsv.php#87120

Example

X::dump(X::toCsv([["John", "Mike", "David", "Clara"],["White", "Red", "Green", "Blue"]])); // John;Mike;David;Clara // White;Red;Green;Blue function(array $data, $delimiter = ';', $enclosure = '"', $separator = ' ', $encloseAll = false, $nullToMysqlNull = false) { $delimiter_esc = preg_quote($delimiter, '/'); $enclosure_esc = preg_quote($enclosure, '/'); $lines = []; foreach ($data as $d) { $output = []; foreach ($d as $field) { if ($field === null && $nullToMysqlNull) { $output[] = 'NULL'; continue; } // Enclose fields containing $delimiter, $enclosure or whitespace if ($encloseAll || preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field)) { $output[] = $enclosure.str_replace($enclosure, '\\'.$enclosure, $field) . $enclosure; } else { $output[] = $field; } } $lines[] = implode($delimiter, $output); } return self::join($lines, $separator); }

Formats an array as a CSV string. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way

This website uses cookies to ensure you get the best experience on our website.