开发者

passing 2-dimensional array in HTTP Request

I'm trying to design a simple API for a webservice I've written. The problem I'm facing is as follows: a form will present to a user any number of themes. This number will change per database.

The user may pick any number of the 开发者_如何学Pythonthemes presented and later on can also assign a number value to them to describe level of relevance.

What I need to do is to be able to pass to the webserivce a 2 dimensional array [themeid: theme value] for each selected theme.

I'm not sure how to do that using HTTP get or POST.

Will appreciate any help!


Why do you need a 2d array? Why not pass a list of pairings back where you use an object to encapsulate a theme id and relevance rating pairing? Here is an example in json:

[
    {
        "rating": 5, 
        "themeId": 1
    }, 
    {
        "rating": 3, 
        "themeId": 2
    }
]


whaley has it right pretty much. You want to use json.

Here is an interactive php session to show you how to do this php->json

php > $root = array();
php > $node = new stdClass();
php > $node->theme = "theme";
php > $node->value = "value";
php > $root[12] = $node;

Here's a print_r of this object, to show you what it looks like in php.

php > print_r($root);
Array
(
    [12] => stdClass Object
        (
            [theme] => theme
            [value] => value
        )

)

Here's an example of the json output.

php > echo json_encode($root);
{"12":{"theme":"theme","value":"value"}}

In essense, you could push this either POST or GET into your application, and json_decode() it to the object in the first section.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜