Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从CSV文件中读取行并导出为JSON_Php_Json_Csv - Fatal编程技术网

Php 从CSV文件中读取行并导出为JSON

Php 从CSV文件中读取行并导出为JSON,php,json,csv,Php,Json,Csv,我想将CSV文件导出到JSON文件,要求在建议字段读取值并写入JSON文件。示例:在第1行中,reconned=2将读取id=2的行,并打印到JSON文件,该文件的值为:id、product_id、title $json = array(); $json['id'] = $row['id']; $json['product_id'] = $row['product_id']; $json['title'] = $row['title'];

我想将CSV文件导出到JSON文件,要求在建议字段读取值并写入JSON文件。示例:在第1行中,reconned=2将读取id=2的行,并打印到JSON文件,该文件的值为:id、product_id、title

$json = array();

        $json['id'] = $row['id'];
        $json['product_id'] = $row['product_id'];
        $json['title'] = $row['title'];
        $json['outline'] = $row['outline'];
        $recom[] = $json;  
print json_encode($recom);

我不知道有什么不同的方法,但下面的方法可以奏效。使用此方法意味着您必须使用列索引,而不是其名称。如果您知道所需值的列索引,这应该不是问题,但我想我会提到:

$fp = fopen('test.csv','r') or die("**! can't open file\n\n");
while($csv_line = fgetcsv($fp,1024)) {
    $id = $csv_line[0];
    $product_id = $csv_line[1];
    $title = $csv_line[2];
    $outline = $csv_line[3];        
}
fclose($fp) or die("**! can't close file\n\n");
现在,因为您将有多行,所以我建议将它们保存到一个JSON对象中:

$fp = fopen('test.csv','r') or die("**! can't open file\n\n");
$i = 0;
while($csv_line = fgetcsv($fp,1024)) {
    $i++;
    $json['json_'.$i]['id'] = $csv_line[0];
    $json['json_'.$i]['product_id'] = $csv_line[1];
    $json['json_'.$i]['title'] = $csv_line[2];
    $json['json_'.$i]['outline'] = $csv_line[3];        
}
$json['total_lines'] = $i;
print json_encode($json);
fclose($fp) or die("**! can't close file\n\n");

使用此方法,您可以将每一行保存为子JSON对象,这样就可以提取总数并解析对象以提取行。

我不知道有什么不同的方法可以这样做,但下面的方法可以工作。使用此方法意味着您必须使用列索引,而不是其名称。如果您知道所需值的列索引,这应该不是问题,但我想我会提到:

$fp = fopen('test.csv','r') or die("**! can't open file\n\n");
while($csv_line = fgetcsv($fp,1024)) {
    $id = $csv_line[0];
    $product_id = $csv_line[1];
    $title = $csv_line[2];
    $outline = $csv_line[3];        
}
fclose($fp) or die("**! can't close file\n\n");
现在,因为您将有多行,所以我建议将它们保存到一个JSON对象中:

$fp = fopen('test.csv','r') or die("**! can't open file\n\n");
$i = 0;
while($csv_line = fgetcsv($fp,1024)) {
    $i++;
    $json['json_'.$i]['id'] = $csv_line[0];
    $json['json_'.$i]['product_id'] = $csv_line[1];
    $json['json_'.$i]['title'] = $csv_line[2];
    $json['json_'.$i]['outline'] = $csv_line[3];        
}
$json['total_lines'] = $i;
print json_encode($json);
fclose($fp) or die("**! can't close file\n\n");

使用此方法,您可以将每一行保存为子JSON对象,这样就可以提取总数并解析对象以提取行。

从这里复制并通过:您的问题不是很清楚,链接也不起作用。这意味着:我想从CSV导出到JSON文件,需要:CSV文件中的1行-->1文件JSON,JSON文件的内容如下:您的问题不是很清楚,链接也不起作用。意思是:我想从CSV导出到JSON文件,要求:CSV文件中的1行-->1文件JSON,JSON文件的内容如下:如何将行内容读写到JSON文件。示例:如果recomment=2,JSON将在CSV文件中显示内容id、标题、产品id、第2行的大纲。我一整天都在尝试,但做不到:(
$I
作为计数器工作,所以从技术上讲,您可以将
if
语句放在
while
循环中。例如:
if($I=2){…}
表示您可以在何处创建JSON对象。但这并不是完全优化的,您最好使用
fgets
之类的工具来提取特定的行,而不是重复所有行。我不明白您的意思,您能与我共享您的代码吗?如何将行的内容读写到JSON文件.Sa中示例:如果recomment=2,JSON将在CSV文件中包含内容id、标题、产品id、第2行的大纲。我整天都在尝试,但做不到:(
$I
用作计数器,所以从技术上讲,您可以将
if
语句放在
while
循环中。例如:
if($I=2){…}
表示您可以在何处创建JSON对象。但这并不是完全优化的,您最好使用
fgets
之类的工具来提取特定的行,而不是重复所有行。我不明白您的意思,您能与我分享您的代码吗?