If you ALLROOM and USERS from the database, then everything works json[j].room or json[j].f but together with one query does not work.
How to remove from a single code only ALLROOM and USERS separately?

Output separately in PHP:

 $pack[] = array('room'=>$row->room,'room2'=>$row->room2 ); echo json_encode($pack); 

JSON separately ALLROOM :

 [{"allroom":[{"room":"1"},{"room2":"2"}] 

JSON separately USERS :

 [{"users":[{"f":"admin1","l":"admin1"},{"f":"admin2","l":"admin2"}]}] 

In PHP, I connected them like this:

 $pack[] = array('allroom'=>$room,'users'=>$users ); echo json_encode($pack); 

JSON immediately two ALLROOM and USERS :

 [{"allroom":[{"room":"1"},{"room2":"2"}],"users":[{"f":"admin1","l":"admin1"},{"f":"admin2","l":"admin2"}]}] 

JS AJAX Code

  $.ajax({ url: '/ajax/stats', type: 'POST', data:({room: 1}), dataType: 'json', cache: false, success: function(json) { var j = 0; while(j <= json.length-1){ $('#stat_users').html(json); j++; } } 
  • json[j].allroom - Artem Gorlachev
  • updated the answer, just checked it seems what you need - Taarim

2 answers 2

Try this

 $.ajax({ url: '/ajax/stats', type: 'POST', data:({room: 1}), dataType: 'json', cache: false, success: function(json) { for (var i in json.users){ $('#stat_users').append(json.users[i]); } console.log(json.allroom); // ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΠΈΡ‚ΡŒ с allroom ΠΏΡ€ΠΈ надобности } 
  • Writes an error VM966: 1 Uncaught SyntaxError: Unexpected token o in JSON at position 1 - Eugene Lisova
  • I do not seem to wake up, try this. JSON.parse doesn’t seem to be necessary in your case - Taarim

 $.ajax({ url: '/ajax/stats', type: 'POST', data:({room: 1}), dataType: 'json', cache: false, success: function(json) { for (var i = 0; i < json[0].allroom.length; i++) { console.log(json[0].allroom[i].name); console.log(json[0].allroom[i].caption); } for (var i = 0; i < json[0].users.length; i++) { for (var j = 0; j < json[0].users[i].length; j++) { console.log(json[0].users[i][j].type) console.log(json[0].users[i][j].name) } } } }); 
 // взял исходный массив ΠΈ попытался ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π΅Π³ΠΎ ΠΏΠΎ ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌ, Π΅Π³ΠΎ Π½ΡƒΠΆΠ½ΠΎ привСсти Π² Ρ‚Π°ΠΊΠΎΠΉ Π²ΠΎΡ‚ Π²ΠΈΠ΄: $pack[] = array( 'allroom' => array( array( 'name' => 'room1', 'caption' => '1' ), array( 'name' => 'room2', 'caption' => '2' ) ), 'users' => array( array( array( 'type' => 'f', 'name' => 'admin1' ), array( 'type' => 'l', 'name' => 'admin1' ) ), array( array( 'type' => 'f', 'name' => 'admin2' ), array( 'type' => 'l', 'name' => 'admin2' ) ), ) ); echo json_encode($pack); 

  • writes an error Uncaught TypeError: Cannot read property 'length' of undefined - Evgeny Lisovoy
  • in the php file instead of $ pack [] = write $ pack =, or in the iteration you will need to write like this - json [0] .allroom.length and accordingly json [0] .allroom [i] .room - RifmaMan
  • Error: Uncaught TypeError: Cannot read property 'f' of undefined I print one right now $ ('# stat_users'). Append (json.users [j] .f); - Yevgeny Lisovoy
  • immediately after receiving the success: function (json) {write console.log (json) and please show what came to your browser console from the php script - RifmaMan
  • Uncaught ReferenceError: json is not defined at <anonymous>: 1: 13 - this is from the console - Evgeny Lisovoy