I have this PHP Array:
Array
(
[messages] => Array
(
[0] => Array
(
[sender] => 17
[receiver] => 4
[message] => TEST 2
[timestamp] => 1367200891
[read] => 0
)
[1] => Array
(
[sender] => 17
[receiver] => 4
[message] => TEST 1
[timestamp] => 1367197661
[read] => 0
)
)
[new_messages] => 2
)
It gets retrieved via an Ajax Request, using json_encode()
to output it in PHP.
function getMessages(rec){
var nmsg = $.ajax({
type: "GET",
async: true,
url: "/ps/getUserMessages.php",
data: {'u':rec,'s00':s00,'n01':n01,'t02':t02}
});
nmsg.done(function(data) {
var json = JSON.parse(data);
if (typeof json.messages[0].message !== 'undefined') {
printMessage(json);
}
});
}
And as printMessage()
function, for example, i need to do something like:
loop(){
$("#elem").append(
'<div>Sender: '+arrayOfValues.sender+'</div>'
+'<div>Message: '+arrayOfValues.message+'</div>'
);
}
for each Array of values.
I tried jQuery $.each()
and also for()
with some examples i found, but i cannot get to work.