0

This is kind of an odd question, but i'll try to explain it as possible. I have this PHP script which fetches data from MySQL. It gets the userID, Username, Email for each user and puts it in an array:

phpoutput = Array ( [id] => 2 [name] => John [lastname] => sena )
Array ( [id] => 3 [name] => human [lastname] => man )

and so on for as many records as there are. The problem is that i want to send this data to jQuery in order to manipulate it with jquery. I know how to send and get data with jQuery, but how can i deal with multiple arrays?

Note : I have already used json_encode() to encode the arrays.

1
  • @nrabinowitz thanks for editing the question much better now ... ;)
    – rainykeys
    Mar 23, 2012 at 19:27

2 Answers 2

0

Generally, you'd put all of your user records into one large array, then send that array to jQuery as JSON:

$array = array(
    array( "id" => "id1", "name" => "John", "lastname" => "Sena"),
    // etc
);
echo json_encode($array);

In jQuery, you'll receive the array, and can loop through the user objects:

$.get('users.php', function(data) {
    var user;
    for (var x = 0; x < data.length; x++) {
        user = data[x];
        console.log(user.id);
    }
});
0
0

use the jquery parseJSON method.

http://api.jquery.com/jQuery.parseJSON/

var obj = jQuery.parseJSON('{"name":"John"}');

alert( obj.name === "John" );

you can console.log obj to make sure its working if you get annoyed with alerts like me.

1
  • i'll take a look into that ... thanks for your help , sure i'll mark it as my answer if it was .. :)
    – rainykeys
    Mar 23, 2012 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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