Home > Article > Backend Development > How can I convert a multi-dimensional PHP array to a CSV file?
Converting an array to a CSV file is a common task in programming. A CSV (comma-separated value) file is a plain text file that stores data in a tabular format, with each row representing a record and each column representing a field.
Consider the following PHP array:
$array = new stdClass(); $array->OrderList_RetrieveByContactResult = new stdClass(); $array->OrderList_RetrieveByContactResult->OrderDetails = new stdClass(); // ... (more properties and values)
To convert the array to a CSV file, follow these steps:
$csv = arrayToCsv($flattenedArray, ',', '"', true, true);
The arrayToCsv function takes the flattened array as an argument and returns a CSV string. The second, third, and fourth arguments specify the delimiter, enclosure, and enclosure all options, respectively. The fifth argument (nullToMysqlNull) converts null values to MySQL's NULL value.
file_put_contents('orders.csv', $csv);
You can now use the CSV file for further processing or analysis.
The above is the detailed content of How can I convert a multi-dimensional PHP array to a CSV file?. For more information, please follow other related articles on the PHP Chinese website!