WP_CLI\Utils

write_csv()WP-CLI 1.0

Write data as CSV to a given file.

No Hooks.

Return

null. Nothing.

Usage

write_csv( $fd, $rows, $headers );
$fd(resource) (required)
File descriptor.
$rows(array) (required)
Array of rows to output.
$headers(array)
List of CSV columns (optional).
Default: []

Code of write_csv() WP-CLI 2.7.0-alpha

function write_csv( $fd, $rows, $headers = [] ) {
	if ( ! empty( $headers ) ) {
		fputcsv( $fd, $headers );
	}

	foreach ( $rows as $row ) {
		if ( ! empty( $headers ) ) {
			$row = pick_fields( $row, $headers );
		}

		fputcsv( $fd, array_values( $row ) );
	}
}