tags:

views:

43

answers:

4

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"}]
+1  A: 

Try this:

$index = count($json) - 1;
echo json_encode($json[$index]);
Jacob Relkin
That worked out pretty well :). So, how exactly would I access the 'sName' value of the first user? I tried 'alert(data[0].sName);' but that is a 'no go'.
Joe
Finding the format of the data is sometimes a pain. Try to figure out what `data` actually is - whether it's an array of objects, or an object itself.
Ryan Kinal
It appears to be an array of objects: [{'a':'1', 'b':'2', 'c':'3'},{'d':'4', 'e':'5', 'f':'6'},{'g':'7', 'h':'8', 'i':'9'}]
Joe
+1  A: 

Move this line to after the fclose:

print_r(json_encode($json));
Joseph
Perfect! And I would access the data as 'data[i].sName;' correct?
Joe
Depends on how the javascript is set up, but that should work.
Joseph
A: 

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();
Marc B
This is a terrible waste of bandwidth and resources. Remove unnecessary parts on the server side instead of encoding, transmitting and decoding them just to throw them away instantly.
Scytale
OP didn't say if he wanted to do the processing serverside or client-side, so I provided the client-side solution.
Marc B
+2  A: 

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.

Scytale
+1 don't know why people are saying print_r when he wants valid json
Matt Williamson