Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

Fixed it. I was echoing $json_array inside the foreach loop.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.