PHP将CSV转换为特定的JSON分布

时间:2018-12-21 15:15:48

标签: php json csv

我有一个将CSV转换为JSON的PHP文件,但我希望输出JSON具有与我现在正在获取的JSON不同的“层次结构”(见下文)。

我的CSV文件看起来像这样:

Icon Name, Responses, Performance  
03_v10_Assembly_point, 225, 38  
55_v10_Fire, 203, 87  
23_v10_Warning_volcano, 324, 79
      

我希望输出的JSON看起来像这样:

{
    "03_v10_Assembly_point" {
            "Responses": "225",
            "Performance": "38"
    },
    "55_v10_Fire" {
            "Responses": "203",
            "Performance": "87"
    },
    "23_v10_Warning_volcano" {
            "Responses": "324",
            "Performance": "79"
    }
}

或者类似的,重要的是将第一列作为其他参数的“标题”。

有没有办法做到这一点? :(

更新18/12/22

尽管问题已经得到解答,但这是我原来的JSON输出:

[
    {
        "Icon": "03_v10_Assembly_point",
        "Responses": "225",
        "Performance": "38",
        "id": 0
    },
    {
        "Icon": "55_v10_Fire",
        "Responses": "203",
        "Performance": "87",
        "id": 1
    },
    {
        "Icon": "23_v10_Warning_volcano",
        "Responses": "324",
        "Performance": "79",
        "id": 2
    }
]

3 个答案:

答案 0 :(得分:0)

您必须逐行阅读csv文件。我逃脱了第一行,因为在您的情况下我们不需要它。

<?php   
$file = fopen('file.csv', 'r'); //read the csv file. Change file.csv to your current file
$i = 0; //initialize $i
$new_arr = array(); //initialize $new_arr
while (($line = fgetcsv($file)) !== FALSE) { //Read each line of the file
    if($i != 0){ //Check if we are on the first line or not
        $new_arr[$line[0]] = array("Responses" => $line[1], "Performance" => $line[2]); //Save info from csv into the new array
    }
    $i++; //Increment by one to escape only the first line
}
fclose($file); //Close file
echo json_encode($new_arr); //Encode result as json and make an echo

输出:

{  
   "03_v10_Assembly_point":{  
      "Responses":" 225",
      "Performance":" 38 "
   },
   "55_v10_Fire":{  
      "Responses":" 203",
      "Performance":" 87 "
   },
   "23_v10_Warning_volcano":{  
      "Responses":" 324",
      "Performance":" 79"
   }
}

答案 1 :(得分:0)

$result = [];

$data = array_slice(array_map('str_getcsv', file('file.csv')), 1); # read csv, skip header

foreach ($data as list($index, $responses, $performance))
{
    $result[$index] = ['Responses' => trim($responses), 'Performance' => trim($performance)];
}

$result = json_encode($result);

答案 2 :(得分:-1)

警告!仅当csv没有任何包含换行符,逗号或用引号引起来的值时,这才有效

也许不是最好的使用方法,但它说明了如何执行此操作。

// Delete first line, and load file using file_get_contents to $csv var
$csv = '03_v10_Assembly_point, 225, 38' . PHP_EOL .
'55_v10_Fire, 203, 87' . PHP_EOL . 
'23_v10_Warning_volcano, 324, 79';

$csv_file = explode(PHP_EOL , $csv);
foreach($csv_file as $line) {
    $csv_array = explode("," , $line);

    $json_key = trim($csv_array[0]);

    $json[$json_key] = [
        'Responses' => trim($csv_array[1]),
        'Performance' => trim($csv_array[2])
    ];

}

print_r($json);

// now create json
$json = json_encode( $json );

结果是

Array
(
    [03_v10_Assembly_point] => Array
        (
            [Responses] =>  225
            [Performance] =>  38
        )

    [55_v10_Fire] => Array
        (
            [Responses] =>  203
            [Performance] =>  87
        )

    [23_v10_Warning_volcano] => Array
        (
            [Responses] =>  324
            [Performance] =>  79
        )

)

{"03_v10_Assembly_point":{"Responses":" 225","Performance":" 38"},"55_v10_Fire":{"Responses":" 203","Performance":" 87"},"23_v10_Warning_volcano":{"Responses":" 324","Performance":" 79"}}