我有一個如下的數組$csvRows
。我想將該數組導出爲CSV。將對象的多維數組導出爲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...)
我已經嘗試過的解決方案,這是我在this link找到。這裏是我的代碼示例:
$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);
然後我收到以下錯誤:
PHP Recoverable fatal error: Object of class Module could not be converted to string ...in line where fputcsv is.
我已經暫時通過手動創建一個簡單的數組像下面這樣,我在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'),
);
但我想導出我的實際$csvRows
對象數組作爲CSV。我是一個新手程序員,所以任何建議和幫助將不勝感激。
嗨,我想你的建議如下所示:'$海峽= json_encode($ csvRows); $ csvRows = json_decode($ str,true); $ fp = fopen(「../../ data/modules.csv」,'w +'); foreach($ csvRows as $ column){ fputcsv($ fp,$ column); } fclose($ fp);'但我只有一個空的csv文件。任何想法? – sumion