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, '\n')) !== 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":"[email protected]","sSSOID":"jdoe","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes cats"}][{"sName":"John Doe","sNick":"John","sEmail":"[email protected]","sSSOID":"jdoe","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes cats"},{"sName":"Bill Frank","sNick":"Bill","sEmail":"[email protected]","sSSOID":"bfrank","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes dogs"}][{"sName":"John Doe","sNick":"John","sEmail":"[email protected]","sSSOID":"jdoe","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes cats"},{"sName":"Bill Frank","sNick":"Bill","sEmail":"[email protected]","sSSOID":"bfrank","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes dogs"},{"sName":"Sam Smith","sNick":"Sam","sEmail":"[email protected]","sSSOID":"ssmith","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes music"}]
The only response I need is the final array:
[{"sName":"John Doe","sNick":"John","sEmail":"[email protected]","sSSOID":"jdoe","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes cats"},{"sName":"Bill Frank","sNick":"Bill","sEmail":"[email protected]","sSSOID":"bfrank","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes dogs"},{"sName":"Sam Smith","sNick":"Sam","sEmail":"[email protected]","sSSOID":"ssmith","sPwd":"Admin1234","sConfPwd":"Admin1234","sDescr":"Likes music"}]