I have an array $csvRows
like the following. I want to export that array as CSV.
Array
(
[0] => SingleModule Object
(
[module:SingleModule:private] => Module Object
(
[name:Module:private] => SimpleXMLElement Object
(
[0] => some string
)
[position:Module:private] => SimpleXMLElement Object
(
[0] => 1000
)
[config:Module:private] => SimpleXMLElement Object
(
)
)
[moduleMeta:SingleModule:private] => ModuleMeta Object
(
[description:ModuleMeta:private] => description text
[featureOrUseCase:ModuleMeta:private] => feature or usecase text
[inputParam:ModuleMeta:private] => input Parameter text
[changedParam:ModuleMeta:private] => changed Parameter text
[newOutputParam:ModuleMeta:private] => new Output Param text
[commentOrNote:ModuleMeta:private] => comments Or Note text
)
)
[1] => SingleModule Object
(etc...)
I have already tried the solution, which I found in this link. Here is my code sample:
$fileName = "../../data/modules.csv";
$output = fopen($fileName, 'w+');
foreach ( $csvRows as $file ) {
$result = [ ];
array_walk_recursive($file, function ($item) use (&$result) {
$result [] = $item;
});
fputcsv($output, $result);
}
fclose($output);
Then I get the following error:
PHP Recoverable fatal error: Object of class Module could not be converted to string ...in line where fputcsv is.
I have temporarily solved the problem by manually creating a straightforward array like the following, which I found in this link:
$csvRows = array(
array( 'row_1_col_1', 'row_1_col_2', 'row_1_col_3' ),
array( 'row_2_col_1', 'row_2_col_2', 'row_2_col_3' ),
array( 'row_3_col_1', 'row_3_col_2', 'row_3_col_3' ),
);
But I want to export my actual $csvRows
array of objects as CSV. I am a newbie programmer, so any suggestion and help would highly appreciated.