在php中将字符串(数组格式)转换为数组

时间:2017-06-21 13:09:32

标签: php arrays

我有一个格式为array的字符串..我必须将它转换为php中的数组

"[["S.No","Date","Particulars","Vch Type","Vch No.","Debit Amount","Credit Amount"],["1","2017-06-20","Purchase-Germany VISA","Card Payment(Kanara)","45","48,879","-"],["2","2017-06-20","Purchase-Thailand VISA","Card Payment(Kanara)","12","46,664","-"],["3","2017-06-20","Purchase-Germany VISA","Card Payment(SBI)","32423","8,558","-"],["4","2017-06-20","Purchase-Germany VISA","CP","3455","21,323","-"],null,["5","2017-06-20","Purchase-Singapore VISA","CP","11111","10,000","-"],["6","2017-06-20","Purchase-Singapore VISA","BP","11111","500","-"],["7","2017-06-20","Purchase-Germany VISA","Card Payment(Kanara)","12345","2,464","-"],["8","2017-06-20","Purchase-Germany VISA","Card Payment(Axis)","12345","1,350","-"]]"

我必须把它放在csv文件中..我试过这个..但是没有用..

 $getdata= "above string";
 $exceldata = json_decode($_REQUEST['getdata'], true); 

 header('Content-Type: text/csv; charset=utf-8');
 header('Content-Disposition: attachment; filename=demo.csv');
 $data = fopen('php://output', 'w');

foreach((array)$exceldata as $arr){
  fputcsv($data,array($arr);
}

2 个答案:

答案 0 :(得分:0)

您未在$data = fopen('php://output', 'w');

上提供CSV文件路径

只需替换

$data = fopen('php://output', 'w');

使用

$data = fopen('file.csv', 'w');

您可以查看fputcsv.php

答案 1 :(得分:0)

你的字符串应该有反斜杠

"[[\"S.No\",\"Date\",\"Particulars\",\"Vch Type\",\"Vch No.\",\"Debit Amount\",\"Credit Amount\"],[\"1\",\"2017-06-20\",\"Purchase-Germany VISA\",\"Card Payment(Kanara)\",\"45\",\"48,879\",\"-\"],[\"2\",\"2017-06-20\",\"Purchase-Thailand VISA\",\"Card Payment(Kanara)\",\"12\",\"46,664\",\"-\"],[\"3\",\"2017-06-20\",\"Purchase-Germany VISA\",\"Card Payment(SBI)\",\"32423\",\"8,558\",\"-\"],[\"4\",\"2017-06-20\",\"Purchase-Germany VISA\",\"CP\",\"3455\",\"21,323\",\"-\"],null,[\"5\",\"2017-06-20\",\"Purchase-Singapore VISA\",\"CP\",\"11111\",\"10,000\",\"-\"],[\"6\",\"2017-06-20\",\"Purchase-Singapore VISA\",\"BP\",\"11111\",\"500\",\"-\"],[\"7\",\"2017-06-20\",\"Purchase-Germany VISA\",\"Card Payment(Kanara)\",\"12345\",\"2,464\",\"-\"],[\"8\",\"2017-06-20\",\"Purchase-Germany VISA\",\"Card Payment(Axis)\",\"12345\",\"1,350\",\"-\"]]";