dongshadu2546 2010-08-11 15:36
浏览 71
已采纳

在PHP中手动构建JSON可编码数组

This is kind of a strange one. I have a CSV that I'm uploading and passing to a PHP script that parses it into arrays, encodes it into a JSON string and sends it back to be further processed by my Javascript. However, I am having difficulty getting the response in a format that is easily parsed. Maybe it can be parsed, I'm just not sure how to access the data within.

PHP script for handling and parsing the CSV:

<?php
 $file = $_FILES['file'];
 $tmp = $file['tmp_name'];
 $row = 1;
 $json = array();
 if(($handle = fopen($tmp, 'r')) !== false){
  while(($data = fgetcsv($handle, 10000, '
')) !== false){
   $num = count($data);
   $row++;

   for($i = 0; $i < $num; $i++){
    $values = split(',', $data[$i]);

    $value = array(
     sName => $values[0],
     sNick => $values[1],
     sEmail => $values[2],
     sSSOID => $values[3],
     sPwd => $values[4],
     sConfPwd => $values[5],
     sDescr => $values[6]
    );

    array_push($json, $value);
   }
   print_r(json_encode($json));
  }
  fclose($handle);
 }
?>

The output ends up being an iterative response where the first array contains the first row of the CSV and the second array contains the first and second rows (the third array contains the first, second, and third, etc. etc.). The very last array is the response that I want. It is a well-formed JSON string; I am just unsure of how to get only that response.

Sample response:

  [{"sName":"John Doe","sNick":"John","sEmail":"jdoe@email.com","sSSOID":"jdoe","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes cats"}][{"sName":"John Doe","sNick":"John","sEmail":"jdoe@email.com","sSSOID":"jdoe","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes cats"},{"sName":"Bill Frank","sNick":"Bill","sEmail":"bfrank@email.com","sSSOID":"bfrank","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes dogs"}][{"sName":"John Doe","sNick":"John","sEmail":"jdoe@email.com","sSSOID":"jdoe","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes cats"},{"sName":"Bill Frank","sNick":"Bill","sEmail":"bfrank@gmail.com","sSSOID":"bfrank","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes dogs"},{"sName":"Sam Smith","sNick":"Sam","sEmail":"ssmith@email.com","sSSOID":"ssmith","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes music"}]

The only response I need is the final array:

[{"sName":"John Doe","sNick":"John","sEmail":"jdoe@email.com","sSSOID":"jdoe","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes cats"},{"sName":"Bill Frank","sNick":"Bill","sEmail":"bfrank@email.com","sSSOID":"bfrank","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes dogs"},{"sName":"Sam Smith","sNick":"Sam","sEmail":"ssmith@email.com","sSSOID":"ssmith","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes music"}]
  • 写回答

4条回答 默认 最新

  • dongru2019 2010-08-11 15:48
    关注

    You might want to use

    $json[] = $value;
    

    instead of array_push and

    echo json_encode($json);
    

    instead of print_r. Also, move the print_r/echo call out of the while loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图