How to Convert XML to CSV using PHP

The example, convert xml to csv using PHP , shows conversion of xml data to csv data. As the title suggests to convert xml to csv using PHP, so we will convert either xml file to csv file or xml string to csv string. When we convert xml string to csv string you will see the output on the browser and when we convert xml file to csv file then you will see the csv document saved into a file on the physical drive location. So here in this example we will see both examples that may help you to choose the example according to your situations.

People working with different types of data may come across the problem that they need to convert between different data formats. Extensible Markup Language (XML) and Comma Separated Values (CSV) are the most widely used formats for data, and conversion between these two formats needs often to be accomplished. Especially to XML, because this format is very well supported by modern applications, and is very well suited for further data manipulation and customization.

Continue reading “How to Convert XML to CSV using PHP”

Read from and write to a csv file using php

This tutorial shows how to read from csv file and write to a csv file using php. In this example I have shown how to read a csv file and display those values. I have also written those csv entries to another csv file.

Code snippets for reading a csv file:

The below code snippets first check if the file exists and has required permission for reading.

Then we need to iterate for each line and print the data using array index. The fgetcsv function retrieves comma separated values into array from each line.

$file_name = "read.csv";
if (($handle = fopen($file_name, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        echo $data[0] or $data[1] or $data[2] etc.
        //more code
    }
    fclose($handle);
}

 

Continue reading “Read from and write to a csv file using php”