在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"}]

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.

Try this:

$index = count($json) - 1;
echo json_encode($json[$index]);

Move this line to after the fclose:

print_r(json_encode($json));

Decode the JSON in your Javascript app, which will produce an array, then use .pop() to get the last value:

var json = json_decode('[{"sName" ......... ');
var lastelement = json.pop();