$ message is an associative array returned after a query to the database

On server

foreach($message as $mes) { $array = array('name' => $mes['name'], 'content' => $mes['content']); echo json_encode($array); } 

On the client

  var message = JSON.parse(this.responseText) 

The problem is that I need to send all messages from the database, via JSON and output them through innerHTML, but it transmits only the first message

    1 answer 1

    This is how it should work:

     $res = array(); foreach( $message as $mes ){ $res[] = array( 'name' => $mes['name'], 'content' => $mes['content'] ); } echo json_encode( $res ); 
    • current without $ res = []; - Zow
    • Updated the answer, correcting errors - timka_s