Already looked through a lot of information on this topic, but I just can’t solve my little problem.

Purpose: obtain data k => v

Returns an array type object from php

 if(isset($_POST['getComment'])) { echo json_encode(array( array( "name" => "Viktor", 'comment' => "коментарий Виктора"), array( "name" => "Петя", "comment" => "коментарий Пети") )); } 

I'm trying to sort it out in js.

  function createFunc(data) { data = JSON.parse(data); // $('body').append("<div id='com'></div>"); for (var i in data) { for (var j in data[i]) { $('#com').append($("<p> " + i +" : " + j + " : "+ data[i][j] + "</p>")); } } } 

It is necessary to refer to the key of one array - get the values ​​separately - the name is

Next is the value of comment . And so with each array in the hierarchy. and get all the values ​​at once. all values ​​in all arrays.

I understand perfectly well that the statement of the question, like the question itself, is a trifling one. But I can't figure it out. Thanks in advance to everyone who took the time to this problem.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

 $(function() { $.ajax({ url: 'index.php', type: 'POST', dataType: 'json' }) .done(function(r) { //v1 for (var i = 0; i < r.length; i++) { $('.res').append(JSON.stringify(r[i]) + '<br>'); } //v2 for (var i = 0; i < r.length; i++) { for (var variable in r[i]) { $('.res').append(variable + ": " + r[i][variable] + "<br>"); } } }) .fail(function() { console.log("error"); }); }); 
  • Thanks for the response, but this is completely different. Displays Each key character and values ​​separately with the array index to which / and the characters in Unicode belong to - mydls1
  • @ mydls1 option 2 - this is the enumeration of all your values k => v - Jean-Claude
  • I guess I did not correctly explain my thoughts. After a thorough search on the site, I found what I was interested in, although it is necessary to finalize a little more ... ru.stackoverflow.com/questions/360791/… - mydls1