2015-09-04 95 views
0

我試圖使用JSON以下數據結構發送到服務器:發送JSON關聯數組到PHP使用jQuery

{"1":{"9":["Foto","45", "39"],"24":["Video","34", "8"]}, 
"2":{"45":["Camara","3", "40"],"7":["Video","96", "7"]}} 

使用推送功能是這樣的:

var a = {}; 
var b = {}; 
var key 

    $("table tr.data").each(function(i) { 
      var row = []; 

      key = i; 

      row.push($(this).find('td').eq(1).text()); 
      row.push($(this).find('td').eq(2).text()); 
      row.push($(this).find('td').eq(3).text()); 

      b[key] = row; 

     }); 

     a[id_valor] = b; 

但我將喜歡創建一個像這樣的關聯數組:

因爲我不知道如何拆分這個數據結構來獲取變量Array a的每個值。

因此,我想創建一個關聯數組,我可以看到哪些數據是什麼並將其發送到服務器。

+0

你有什麼'key'變量? – Nit

回答

0

我不知道我是否正確答應了......但是您有一張表,其中包含表格行與類數據。要通過這些行中循環,並創建一個JSON發送到服務器...

,如果是這樣的話 - 在這裏,我們去:

var myTable = {}; //declare a table object 
$(".data").each(function(index) { //loop through each .data row, passing the current row index into the function 
    var tableRow = []; //create row array for current row 

    $(this).find("td").each(function(){ // loop through every table cell 
     tableRow.push($(this).text()); //add the content of the td to the tablerow 
    }); 

    myTable[index] = tableRow; //add the table row array to the table object with 'index' as key 
}); 

var myString = JSON.stringify(myTable); //create JSON out of table object 

看到這裏的工作小提琴:http://jsfiddle.net/mdke87k1/

我包括一個'輸出div',我把結果json放進去。 據我可以看到它 - 它不同於你的,因爲外括號丟失。但我想這應該有助於讓你開始。

+0

是的,謝謝@errand我已經知道了,我公開我的下一個問題,如何在PHP中分析(讀取,拆分)此數組的內容以創建多個查詢等並將答案發回給客戶端(jQuery) –