0

我想将一个数组从 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

提前感谢您的每一个回复!

4

3 回答 3

2

json_decode将为编码为 JSON 的对象创建对象。如果您想要一个关联数组,请true作为第二个参数传递:

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

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

于 2013-05-03T12:47:49.223 回答
0
Its json_decode() not json.decode()

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

如果你print_r($resultArray) 你会得到一个标准的类数组,你可以通过它访问它 echo $resultArray[0]->id并且会给你 1;

于 2013-05-03T12:47:12.673 回答
0

json_decode,不是json.decode

于 2013-05-03T12:47:21.980 回答