lib/blobfolio/common/format.php
public static function to_csv($data=null, $headers=null, string $delimiter=',', string $eol="\n") {
		ref\cast::array($data);
		$data = \array_values(\array_filter($data, 'is_array'));
		ref\cast::array($headers);

		$out = array();

		// Grab headers from data?
		if (
			! \count($headers) &&
			\count($data) &&
			(cast::array_type($data[0]) === 'associative')
		) {
			$headers = \array_keys($data[0]);
		}

		// Output headers, if applicable.
		if (\count($headers)) {
			foreach ($headers as $k=>$v) {
				ref\cast::string($headers[$k], true);
			}

			ref\sanitize::csv($headers);

			$out[] = '"' . \implode('"' . $delimiter . '"', $headers) . '"';
		}

		// Output data.
		if (\count($data)) {
			foreach ($data as $line) {
				foreach ($line as $k=>$v) {
					ref\cast::string($line[$k], true);
				}

				ref\sanitize::csv($line);

				$out[] = '"' . \implode('"' . $delimiter . '"', $line) . '"';
			}
		}

		return \implode($eol, $out);
	}
public static function to_csv($data=null, $headers=null, string $delimiter=',', string $eol="\n") {
		ref\cast::array($data);
		$data = \array_values(\array_filter($data, 'is_array'));
		ref\cast::array($headers);

		$out = array();

		// Grab headers from data?
		if (
			! \count($headers) &&
			\count($data) &&
			(cast::array_type($data[0]) === 'associative')
		) {
			$headers = \array_keys($data[0]);
		}

		// Output headers, if applicable.
		if (\count($headers)) {
			foreach ($headers as $k=>$v) {
				ref\cast::string($headers[$k], true);
			}

			ref\sanitize::csv($headers);

			$out[] = '"' . \implode('"' . $delimiter . '"', $headers) . '"';
		}

		// Output data.
		if (\count($data)) {
			foreach ($data as $line) {
				foreach ($line as $k=>$v) {
					ref\cast::string($line[$k], true);
				}

				ref\sanitize::csv($line);

				$out[] = '"' . \implode('"' . $delimiter . '"', $line) . '"';
			}
		}

		return \implode($eol, $out);
	}
public static function to_csv ( $ data = null , $ headers = null , string $ delimiter = ',' , string $ eol = "\n" ) { ref \ cast :: array ( $ data ) ; $ data = \ array_values ( \ array_filter ( $ data , 'is_array' ) ) ; ref \ cast :: array ( $ headers ) ; $ out = array ( ) ; // Grab headers from data? if ( ! \ count ( $ headers ) && \ count ( $ data ) && ( cast :: array_type ( $ data [ 0 ] ) === 'associative' ) ) { $ headers = \ array_keys ( $ data [ 0 ] ) ; } // Output headers, if applicable. if ( \ count ( $ headers ) ) { foreach ( $ headers as $ k => $ v ) { ref \ cast :: string ( $ headers [ $ k ] , true ) ; } ref \ sanitize :: csv ( $ headers ) ; $ out [ ] = '"' . \ implode ( '"' . $ delimiter . '"' , $ headers ) . '"' ; } // Output data. if ( \ count ( $ data ) ) { foreach ( $ data as $ line ) { foreach ( $ line as $ k => $ v ) { ref \ cast :: string ( $ line [ $ k ] , true ) ; } ref \ sanitize :: csv ( $ line ) ; $ out [ ] = '"' . \ implode ( '"' . $ delimiter . '"' , $ line ) . '"' ; } } return \ implode ( $ eol , $ out ) ; }
Generate CSV from Data

@param array $data Data (row=>cells).
@param array $headers Headers.
@param string $delimiter Delimiter.
@param string $eol Line ending type.
@return string CSV content.
Generate CSV from Data