【问题标题】:php script to read csv file to json with static header before itemsphp脚本在项目之前将csv文件读取到带有静态标题的json
【发布时间】:2021-05-23 06:03:44
【问题描述】:

这是我的 CSV 数据

Shulename Shuleid systemTransId transactionDate sttId gender
mzimu 01-022-1 20-007440 18/05/2021 12 F
mzimu 01-022-1 20-007441 18/05/2021 13 M
mzimu 01-022-1 20-007442 19/05/2021 14 M

这是我的预期结果

{
   "shulename": "mzimu",
   "shuleid": "01-022-1",
   "items": 
  [
         {
           "systemTransId": "20-007440",
           "transactionDate": "18/05/2021",
           "sttId": "12",
           "gender": "F"
         },
         {
           "systemTransId": "20-007441",
           "transactionDate": "18/05/2021",
           "sttId": "13",
           "gender": "M"

         },
         {
           "systemTransId": "20-007442",
           "transactionDate": "19/05/2021",
           "sttId": "14",
           "gender": "M"

         }
    ]
 }

【问题讨论】:

  • 您有多个shulename 还是总是mzimu?到目前为止,您还尝试过什么?你能把csv读入php吗?
  • 问题是什么?
  • 是的 Shulename 和 shuleid 永远不会改变
  • 我能够读取 csv,但不是我想要的那种静态标题(shuleid,shulename)他们加入数据
  • @Kinglish 我想要一个可以从 csv 文件生成 json 数据但格式为该格式的脚本

标签: php json csv


【解决方案1】:

你可以在这里看到它的工作演示:https://www.tehplayground.com/400X1OR0vepmj7o6

我伪造了 csv 流来展示如何将其与 fgetcsv 一起使用

$csv = "Shulename,Shuleid,systemTransId,transactionDate,sttId,gender
mzimu,01-022-1,20-007440,18/05/2021,12,F
mzimu,01-022-1,20-007441,18/05/2021,13,M
mzimu,01-022-1,20-007442,19/05/2021,14,M";

//$file = fopen("YOURFILE.csv","r");

$file     = fopen('data://text/plain,' . $csv,'r');
$line = 0 ;
$keys = [];
while (($data = fgetcsv($file)) !== FALSE)
{
   if ($line==0) {
       // set up the array
       foreach ($data as $d) $keys[]=strtolower($d);
       
       $output = array(
           $keys[0] => "", 
           $keys[1] => "",
           "items" => array()
           );
   } else {
        if ($line==1) {
            $output[$keys[0]] = $data[0];
            $output[$keys[1]] = $data[1];
        }
        $tmp = [];
        for ($x=2; $x<count($keys); $x++) {
            $tmp[$keys[$x]] = $data[$x];
        }   
        $output['items'][]=$tmp;
   }
   $line++;
}

$output = json_encode($output, JSON_PRETTY_PRINT);


die("<pre>".print_r($output,1));

【讨论】:

    【解决方案2】:

    根据您的评论,如果我理解正确,shulename 将始终为 mzimushuleid 将始终为 01-022-1

    因此从您编写的代码继续创建您想要的格式是

    
    if (($handle = fopen('revenue.csv', 'r')) === false) {
        die('Error opening file');
    }
    $headers = fgetcsv($handle, 1024, ',');
    $complete = array();
    while ($row = fgetcsv($handle, 1024, ',')) {
        $complete[] = array_combine($headers, $row);
    }
    fclose($handle);
    
    // We have shulename and shuleid assigned statically
    // because you said they will always be the same
    
    $requiredFormat = ['shulename' => 'mzimu',
        'shuleid' => '01-022-1'];
    foreach ($complete as $item) {
        ## Notice the case here. The csv in the question is ucfirst and the required format is lower case
        ## php indexes are case sensitive
        unset($item['Shulename']);
        unset($item['Shuleid']);
        $requiredFormat['items'][] = $item;
    }
    echo json_encode($requiredFormat);
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      相关资源
      最近更新 更多