Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to pass a array of array from javascript into php. what is the simplest way to do it?

Array in javascript likes:

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);

in the php I use:

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

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

Thanks in advance for every reply!

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted
Its json_decode() not json.decode()

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

if you print_r($resultArray) you will get a standard class array and you can access it by echo $resultArray[0]->id and will give you 1;

share|improve this answer
 
yes, it works. thanks –  cecilfang May 3 '13 at 12:57
 
Yuo are welocme @cecilfang –  웃웃웃웃웃 May 3 '13 at 13:02
add comment

json_decode will create objects for objects encoded as JSON. If you want an associative array instead, pass true as second argument:

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

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

share|improve this answer
 
yes, it works. thanks –  cecilfang May 3 '13 at 12:58
 
why can I not choose the both solutions as the answer _, sry! –  cecilfang May 3 '13 at 13:11
add comment

It's json_decode, not json.decode

share|improve this answer
 
sry, it was typo –  cecilfang May 3 '13 at 12:54
 
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. –  Shikiryu May 3 '13 at 13:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.