在PHP中将数据从数组格式化为Json

为了向 AJAX 请求发送数据,我需要发送格式如下的数据。


{

    "columns": [

        [ "Name" ],

        [ "Position" ],

        [ "Office" ],

        [ "Extn." ],

        [ "Start date" ],

        [ "Salary" ]

    ],

    "data": [

    [

      "Tiger Nixon",

      "System Architect",

      "Edinburgh",

      "5421",

      "2011/04/25",

      "$320800"

    ],

    [

      "Garrett Winters",

      "Accountant",

      "Tokyo",

      "8422",

      "2011/07/25",

      "$170750"

    ]

  ]

}

如果我将此代码放入 .txt 中,则请求有效,但我的代码的目的是使用 CSV 文件中的数据。这就是我使用此 file.php 的原因:


<?php


//import csv file

if(!empty($_FILES['csv_file']['name']))

{

$file_data = fopen($_FILES['csv_file']['tmp_name'], 'r');

  $csv = fgetcsv($file_data);

  //get header column  of csv file : 1rst line

  $heads = explode(';', $csv[0]);

    foreach ($heads as $head) {

     $json['columns'][] = [$head];

  }

//get  rows of csv file

 fgetcsv($file_data);

 while($row = fgetcsv($file_data))

 {

  $json['data'][] = array(

   $row[0],

   $row[1],

   $row[2],

   $row[3],

   $row[4],

   $row[5],

   $row[6],

   $row[7],

   $row[8],

   $row[9],

   $row[10],

  );

 }

echo json_encode($json);

 ?>

你能告诉我我的代码有什么问题吗?


编辑:这是file.csv(数据源)


Name,Position,Office,Extn.,Start date,Salary

Tiger NixonSystem Architect,,Edinburgh,5421,25/04/2011,$320800

Garrett Winters,Accountant,Tokyo,8422,25/07/2011,$170750


一只名叫tom的猫
浏览 101回答 1
1回答

烙印99

我已经修改了你的代码。请尝试一下。它正在工作。我有删除标题foreach。这不是必需的。此外,在 2ed 循环中删除一对一的数组分配代码并添加$json['data'][] = explode(';', $row[0]);代码。这是修改后的代码。<?php//import csv fileif (!empty($_FILES['csv_file']['name'])) {&nbsp; &nbsp; $file_data = fopen($_FILES['csv_file']['tmp_name'], 'r');&nbsp; &nbsp; $csv = fgetcsv($file_data);&nbsp; &nbsp; //get header column&nbsp; of csv file : 1rst line&nbsp; &nbsp; $json['columns'] = $csv;&nbsp; &nbsp; //get rows of csv file&nbsp; &nbsp; while ($row = fgetcsv($file_data)) {&nbsp; &nbsp; &nbsp; &nbsp; $json['data'][] = $row;&nbsp; &nbsp; }&nbsp; &nbsp; echo json_encode($json);}?>=> 输出大批:Array(&nbsp; &nbsp; [columns] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Position&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => Office&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => Extn.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [4] => Start date&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [5] => Salary&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [data] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Tiger NixonSystem Architect&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => Edinburgh&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 5421&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [4] => 25/04/2011&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [5] => $320800&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Garrett Winters&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Accountant&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => Tokyo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 8422&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [4] => 25/07/2011&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [5] => $170750&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))JSON:{"columns":["Name","Position","Office","Extn.","Start date","Salary"],"data":[["Tiger NixonSystem Architect","","Edinburgh","5421","25\/04\/2011","$320800"],["Garrett Winters","Accountant","Tokyo","8422","25\/07\/2011","$170750"]]}
打开App,查看更多内容
随时随地看视频慕课网APP