2014-10-17 136 views
0

您好我有PHP代碼例如:轉換PHP數組的JavaScript陣列

$array1 = array("fruits" => "banana","vegetables" => "tomatos"); 
$array2 = array("name" => "Jack","Age" => "32"); 

$array3 = array($array1, $array2); 

echo json_encode($array3); 

JavaScript代碼包括jquery的:

var json_data = $.ajax({ 
       type: 'POST', 
       url: 'scripts/myfile.php', 
       data: { action: 'myaction' }, 
       dataType: 'json', 
       cache: false, 
       success: function(result) { 
        console.log(result); 
       } 
      }); 

如何json_data轉換爲陣列和影響兩個$ array1 & $ array2到javascript數組!

這是json_data內容:

"{"HUM":[{"label":"2014-10-16 17:08:55","y":"58"},{"label":"2014-10-15 08:16:55","y":"56"},{"label":"2014-10-15 08:16:50","y":"56"},{"label":"2014-10-15 08:16:45","y":"56"},{"label":"2014-10-15 08:16:40","y":"56"},{"label":"2014-10-15 08:16:35","y":"56"},{"label":"2014-10-15 08:16:30","y":"56"},{"label":"2014-10-15 08:16:25","y":"56"},{"label":"2014-10-15 08:16:20","y":"56"},{"label":"2014-10-15 08:16:15","y":"56"}, 

"TEMP":[{"label":"2014-10-16 17:08:55","y":"26"},{"label":"2014-10-15 08:16:55","y":"24"},{"label":"2014-10-15 08:16:50","y":"24"},{"label":"2014-10-15 08:16:45","y":"24"},{"label":"2014-10-15 08:16:40","y":"24"},{"label":"2014-10-15 08:16:35","y":"24"},{"label":"2014-10-15 08:16:30","y":"24"},{"label":"2014-10-15 08:16:25","y":"24"},{"label":"2014-10-15 08:16:20","y":"24"},{"label":"2014-10-15 08:16:15","y":"24"}, 

"HUM2":[{"label":"2014-10-16 17:08:55","y":"38"},{"label":"2014-10-15 08:16:55","y":"36"},{"label":"2014-10-15 08:16:50","y":"36"},{"label":"2014-10-15 08:16:45","y":"36"},{"label":"2014-10-15 08:16:40","y":"36"},{"label":"2014-10-15 08:16:35","y":"36"},{"label":"2014-10-15 08:16:30","y":"36"},{"label":"2014-10-15 08:16:25","y":"36"},{"label":"2014-10-15 08:16:20","y":"36"},{"label":"2014-10-15 08:16:15","y":"36"},{"label":"2014-10-15 08:16:10","y":"36"}, 

"TEMP2":[{"label":"2014-10-16 17:08:55","y":"23"},{"label":"2014-10-15 08:16:55","y":"24"},{"label":"2014-10-15 08:16:50","y":"24"},{"label":"2014-10-15 08:16:45","y":"24"},{"label":"2014-10-15 08:16:40","y":"24"},{"label":"2014-10-15 08:16:35","y":"24"},{"label":"2014-10-15 08:16:30","y":"24"},{"label":"2014-10-15 08:16:25","y":"24"},{"label":"2014-10-15 08:16:20","y":"24"},{"label":"2014-10-15 08:16:15","y":"24"},{"label":"2014-10-15 08:16:10","y":"24"},{"label":"2014-10-15 08:16:05","y":"24"},{"label":"2014-10-15 08:16:00","y":"24"}]}" 

現在我想將其轉換爲數組,然後serparate陣列HUM - TEMP - HUM2 - TEMP2

+0

'OBJ = JSON.parse(結果);'' – 2014-10-17 11:48:47

+0

OBJ = $ .parseJSON(結果);' – 2014-10-17 11:50:04

+0

它給出了錯誤:未捕獲的SyntaxError:意外令牌ö – Souf 2014-10-17 11:52:45

回答

0

jQuery $.ajax()文檔上dataType當它被設置爲json

"json": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

對象結果已經是成功函數中的JSON對象。所以你可以簡單地使用result[0]['fruits']result[1]['name']來訪問相關的數組。

編輯:更正的索引爲@charlietfl指出的。

+0

不正確,OP合併數組的方式....將會是'result [0] ['fruits ']'或'result [1] ['name']' – charlietfl 2014-10-17 12:31:08

0

$array3 = array(array1, array2);應該$array3 = array($array1, $array2);

1

在你的代碼,

$array3 = array(array1, array2); 

這裏數組1和數組2視爲字符串時沒有引號,這樣你就不會得到你的答案。這應該是PHP變量。您必須包含$登錄。

$array3 = array($array1, $array2);