簡體   English   中英

如何在沒有ajax的情況下將數組數組從javascript傳遞到php?

[英]How to pass array of array from javascript into php without ajax?

我想將一個數組數組從javascript傳遞到php。 最簡單的方法是什么?

javascript中的數組喜歡:

var resultArray = [ 
                    {"id":"1", "description":"aaa", "name":"zzz", "titel":"mmm"},
                    {"id":"2", "description":"bbb", "name":"yyy", "titel":"nnn"},
                    {"id":"3", "description":"ccc", "name":"xxx", "titel":"lll"},
                    {"id":"4", "description":"ddd", "name":"www", "titel":"qqq"},
                    {"id":"5", "description":"eee", "name":"vvv", "titel":"rrr"}
                  ] 

windows.location.href = "searchResults.php?resultArray=" + JSON.stringify(resultArray);

在我使用的PHP中:

$resultArray = json_decode($_GET['resultArray']);

echo $resultArray[0]['id']; // should be "1", but show nothing

每個回復都要提前感謝!

json_decode將為編碼為JSON的對象創建對象。 如果您想要一個關聯數組,請將true作為第二個參數傳遞:

$resultArray = json_decode($_GET['resultArray'], true);

參考: http//php.net/manual/en/function.json-decode.php

Its json_decode() not json.decode()

$resultArray = json_decode($_GET['resultArray']);

如果你print_r($resultArray )你會得到一個標准的類數組,你可以通過echo $resultArray[0]->id訪問它,它會給你1;

它是json_decode ,而不是json.decode

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM