I have the following in php:
$follow=explode(" ",$_SESSION['Following']); //create array from the string stored in session variable
foreach($follow as $val) {
$show = $val;
//my query
$result=mysqli_query($dbc,$query);
WHILE ($rows = mysqli_fetch_assoc($result)) {
//$array[]= $rows; // tried this
//$array=json_encode($rows); //tried this
//array_push($array,$rows); // tried this
}
$json_array=json_encode($array);
echo $json_array;
If I take a single pass through the foreach loop the json object looks like this: [{key:value}....], which can be parsed in my javascript. However, with multiple passes in the foreach I am getting multiple arrays within the object ,like this: [{key:value}][{key:value}]..... which results in the following SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data, which I guess are the []'s inside the object. How can I create the json object in the foreach loop to fix this?