frankdobbs
frankdobbs

Reputation: 1

extracting multidimension json array in javascript

I am reading a json response from a server which is sending a multidimensional array from a sqlite database using the following php code:

while ($data = $result->fetchArray(SQLITE_NUM)) {
echo json_encode($data);

This works Ok and sends its data out as a string in the following format: [1,"Board1","Floor1",1,100,1,"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10"] [1,"Board2","Floor2",1,100,1,"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10"] [1,"Board3","Floor3",1,100,1,"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10"]

I am using jQuery to request this data from the server using the following code and want to save the recieved data in a javascript array so that I can access individual items using for example data[0][1] would to refer to the "Board2" element - but cannot seem to do this?

The code works with a single array, but not with multi-arrays?

JAVASCRIPT CODE:

function requestMtrConfig() {
$.ajax({
    url: '/php/getmtrconfig.php',
    async:false,                    // synchronous read
    success: function(datas) {          
               data[0] = datas[0];
             data[1] = datas[1];
             data[2] = datas[2];
        }

    },       
    cache: false
});
}

How can I put the returned json array into a multi-dimensioned javascript array? PS: I have already created a global multidimensioned JavaScript array variable called data.

Any help would be much appreciated thanks Frank dobbs

Upvotes: 0

Views: 219

Answers (1)

awm
awm

Reputation: 6570

The problem is that you're sending out several pieces of JSON without joining them together. That's not the same as sending out a JSON array.

Suggested remedy:

$json = array();
while ($data = $result->fetchArray(SQLITE_NUM)) {
  $json[] = $data;
}
echo json_encode($json);

Upvotes: 2

Related Questions