Working with simple Array to CSV, XML and XLS Class in PHP

Most of the Array to CSV, XML and XLS are having different function or class, So we have though of writing all in same class. Class support different type of format like CSV, XML and XLS.

Class is design as calling the static method instead of creating the object, as the current version we have support limited methods.

How it’s work

It’s work based on the format type and date which have header and rows in array format, Once you call the getData function it will communicate the method based on the format type. Then set the header part to download the file.

Setting Up the Data

Three arguments accept in getData function.

  1. fileName – which will used to download the file with given name.
  2. formatType – used to set the format type for the data.
  3. Data –  use to send the data in header and rows in array format,

arrayToFormat.php

<?php class arrayToFormat {	

	// creating csv function which will accept array format
	static function csv($arr) { 
	
		// setting the header array
    if(is_array($arr)) {
		$strCsvOutput = implode(",",$arr[0])." \n";
		$intCnt = count($arr[1]);
		
		// adding the rows values into $strCsvOutput string
		for($i=0;$i<$intCnt;$i++) {
			$strCsvOutput .= implode(",",$arr[1][$i])." \n";
		}		
		return $strCsvOutput;
    }
	}
	
	// creating xml function which will accept array format
	static function xml($arr) {
	
		// creating the xml node
		$strXmlOutput  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		$strXmlOutput .= "<data>";
		
		$i = 0;
		
		// generating the value for the xml node
		foreach ($arr[1] as $key=>$value) {
		
			// creating xml node from the array
			$strXmlOutput .= "<row>";
			foreach ($arr[0] as $k=>$v) {
				$strXmlOutput .= "<$v>".$arr[1][$i][$k]."</$v>";
			}
			$strXmlOutput .= "</row>";			
			$i = $i + 1;
		}
		
		$strXmlOutput .= "</data>";
		return $strXmlOutput;
	}
	
	// creating xls function which will accept array format
	static function xls($arr) {
	
		// setting the header array
		$strXlsOutput = implode(",",$arr[0])." \n";
		$intCnt = count($arr[1]);
		
		// adding the rows values into $strXlsOutput string		
		for($i=0;$i<$intCnt;$i++) {
			$strXlsOutput .= implode(",",$arr[1][$i])." \n";
		}		
		return $strXlsOutput;
	}
	
	// creating header function which will accept filename and format type
	static function getHeaderType($fileName,$type = 'csv') {
		switch($type) {
			case "csv":
				header('Content-type: text/csv; charset=utf-8');
				header("Content-disposition: attachment;filename={$fileName}.csv");
			break;
			
			case "xml":
				header("Content-type: text/xml; charset=utf-8");
				header("Content-disposition: attachment;filename={$fileName}.xml");
			break;
			
			case "xls":
				header('application/vnd.ms-excel; charset=utf-8');
				header("Content-disposition: attachment;filename={$fileName}.xls");	
			break;
		}
	}

	// calling the getHeaderType and type based method from the arguments
	static function getData($fileName,$type = 'csv', $arr = array()) {	
		self::getHeaderType($fileName,$type);
		echo self::$type($arr);
	}
}

Calling the method and assigning the data into it.

<?php
// setting the header and rows part in array format
$arrHeader = array("field1","field2","field3","field4");
$arrData = array(array(10,202,10,22),array(1,20,1,2));

$arr = array($arrHeader,$arrData);

// assigning the filename, format type and array
convertData::getData('test','xls',$arr);