I think you are having issues not with encoding/decoding your array, but how you are actually creating your array. In your while loop, you are creating $response and setting it to the current row. Ultimately, this will only provide you with the last row that your query processes (which is fine if you know that there will be only one row returned). To avoid this, declare an empty array: $response = array();. Make sure that $response['data'] is not overwritten multiple times by properly making it multidemensional: $response['data'][]. This ensures each row is appended to the array and does not overwrite a value already stored. Try the following out for your JSON functionality:
<?php
$query = "SELECT * FROM students";
$result = mysqli_query($con, $query);
$response = array();
while($rows = mysqli_fetch_array($result)) {
$fullrecord = $rows['ID'] . " " . $rows['StudentName'] . " " . $rows['Age'];
$response['data'][] = $fullrecord;
}
$response = json_encode($response);
deliver_response($_GET['format'], $response);
?>
UPDATE PER YOUR COMMENT:
The following code will simulate your query. It will use $table which is just a simple array. Each item in it will simulate your rows in your db table. It will loop through each item in the array and push it into a new associative array called $response. This is directly related to your problem so pay attention here. Once the loop is done (all rows are pushed into $response), we encode it and return it:
<?php
// this simulates your db table
$table = array('row 1', 'row 2', 'row 3', 'row 4');
$response = array();
// this simulates your `while` loop
foreach ($table as $row) {
// push your value into the associative array
$response['data'][] = $row;
}
// print_r will yield the following:
// Array ( [data] => Array ( [0] => row 1 [1] => row 2
// [2] => row 3 [3] => row 4 ) )
$json_response = json_encode($response);
// another print_r will yield the following:
// {"data":["row 1","row 2","row 3","row 4"]}
return $json_response;
?>
Now that we have the JSON data, we can read it on the other side with using something similar to what follows. First we will decode it, grab the array [data], and we will loop through it to print the results on the screen:
<?php
$response = json_decode($json_response);
// pull the data:
$data = $response->{'data'};
foreach ($data as $d) {
echo $d . '<br />';
}
?>
$json = json_encode($your_array);print_rto see it if you want to debug.